Boost logo

Boost :

From: John Maddock (John_Maddock_at_[hidden])
Date: 2001-06-12 06:26:05

>I want to use a regular expression that can pick two separate fields
>in the above message. For example I wish to search for messages that
>include "tx_seq_num = 3" AND "ack_seq_num = 2" but I am not
>interested to see messages that include only one of the two.

That's not something that regex's do, or at least I've never seen one that
does that.

>A way I thought was to use an ORed expression
>i.e. "tx_seq_num=3|ack_seq_num = 2" (which works fine btw) and then
>check [what] to see what was matched, if both fields were matched
>then I want to see the message else discard it.
>But I am not very sure if in that case the algorithm after finding
>the first match tries to look for the second or returns true with the
>first match (possibly for optimization issues).

It returns the first thing that matches, in your example case that would be
the "tx_seq_num = 3" field.

One way to do what you want would be to use regex_grep, and keep a track or
how many unique matches were found - in fact if each field occures only
once regex_grep does this for you as it returns the total number of matches

// this regex assumes that each field starts a new line (speeds up
boost::regex re("^(?:tx_seq_num|ack_seq_num)\\s*=\\s*\\d+");

// this predicate stops the grep once we've found N matches
// (optimisation: there's no point in continuing once we've found as many
as we want),
// we could also store the matches if that was desired:
struct counting_predicate
        unsigned target;
        unsigned count;
        counting_predicate(int i) : target(i), count(0) {}
        bool operator()(const
        { return ++count < target; }

bool check_message(const std::string& msg)
        counting_predicate pred(2);
        return (regex_grep(pred, msg, re) > 1);

- John Maddock

Boost list run by bdawes at, gregod at, cpdaniel at, john at