Boost logo

Boost Users :

From: Eric Niebler (eric_at_[hidden])
Date: 2005-04-01 13:23:36


jordi wrote:
>
> I would like to know if it's possible to assign an alias/name to a
> sub-expression. When using short regex there is no problem nor
> confusion but when using a long regex then I get lost looking at the
> correct index to use. It would be much easier if I can assign an alias.
>
> So, instead to use a regex like this:
>
> "\s*(\d+)\s+(\d+)\s+(\d+)\s+"
>
> I would like to be able to use something like:
>
> "\s*([$VALUE1]\d+)\s+([$VALUE2]\d+)\s+([$VALUE3]\d+)\s+"
>

Have a look at xpressive in boost-sandbox. It's a regex library with a
interface similar to Boost.Regex, and it lets you write regexes as
expression templates, which can call other regexes. You should be able
to do this:

using namespace boost::xpressive;

sregex value1, value2, value3;
// ... initialize value1, value2, value3

sregex rex = *_s >> (s1= value1 >> +_d)
>> +_s >> (s2= value2 >> +_d)
>> +_s >> (s3= value3 >> +_d)
>> _s
            ;

This creates a regex rex that refers to three other regexes: value1,
value2 and value3. (The s1=, s2= and s3= are for capturing
backreferences and are unrelated to the nested regex functionality. You
can remove them if you're not interested in backreferences.)

xpressive docs:
http://boost-sandbox.sf.net/libs/xpressive

xpressive download:
http://boost-sandbox.sourceforge.net/vault/index.php?directory=eric_niebler

(Caveat, xpressive is still under development, although it is quite
usable at this point.)

-- 
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