Boost logo

Boost Users :

From: Eric Niebler (eric_at_[hidden])
Date: 2005-09-26 10:31:37


Cory Nelson wrote:
> I'm attempting to learn xpressive by writing a simple IRC regex.
>
> The IRC protocol goes like:
> :prefix numericreplyorcommandstring param1 param2 :trailing param
>
> The regex looks like this (ignore comments and newlines):
> ^
> (?::(\S+)\s)? // prefix
> (?:(\d+)|(\S+)) // command
> (?:\s([^:]\S*))* // parameters
> (?:\s:(.+))? // trailing parameter
> $
>
> And my static xpressive looks like this:
> bos >>
> optional(':' >> (s1=+~_s) >> _s) >> // prefix
> ((s2=+_d)|(s3=+~_s)) >> // command
> *(_s >> (s4 = ~as_xpr(':') >> *~_s)) >> // parameters
> optional(_s >> ':' >> (s5=+_)) >> // trailing parameter
> eos
>
> I'm not sure how to retrieve the multiple matches for s4 - any help?
>

In this regard, xpressive behaves like Perl and like Boost.Regex. For
captures in repeats, only the last iteration is remembered.

If you want to remember all the places s4 matches, you should use a
nested regex instead:

sregex param = ~as_xpr(':') >> keep(*~_s);
sregex protocol =
   bos >>
   optional(':' >> (s1=+~_s) >> _s) >> // prefix
   ((s2=+_d)|(s3=+~_s)) >> // command
   *(_s >> parameter) >> // parameters
   optional(_s >> ':' >> (s5=+_)) >> // trailing parameter
   eos;

Now, when you match "protocol", the match_results struct will contain
nested matches for the "parameter" regex, which you can access with
match_results<>::nested_results().

(You have nested quantifiers in your pattern. I tried to make it more
efficient with the keep() directive.)

HTH,

-- 
Eric Niebler
Boost Consulting
www.boost-consulting.com

Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net