views:

44

answers:

2

I am needing to implement regular expressions in a C++ program I am writing, and I wanted to use re2 but I could not compile it on windows. Does anyone know of another regular expression library or whatever it's called that compiles easily on windows and isn't a "backtracking" regex engine, but an automata-theory based one (whatever that means) like re2 is?

Or just figuring out how to compile re2 on windows would be perfect.

+1  A: 

Regular expressions are part of the TR1 standard, so chances are you already have a <tr1/regex> header that contains a std::tr1::regex class and related functions.

sth
I'm not knowledgeable at all in this stuff, is that as fast as re2?
Langley
And I'm using Microsoft Visual C++, and I don't seem to have a <tr1/regex> file.
Langley
Wow, should have read the link first.
Langley
A: 

Have a look at

http://www.complang.org/ragel/

It's an external DSL so not technically C++. However as it generates pure C++ / C from the regular expressions it should be much faster than anything that is built at runtime./

For example.

action dgt      { printf("DGT: %c\n", fc); }
action dec      { printf("DEC: .\n"); }
action exp      { printf("EXP: %c\n", fc); }
action exp_sign { printf("SGN: %c\n", fc); }
action number   { /*NUMBER*/ }

number = (
    [0-9]+ $dgt ( '.' @dec [0-9]+ $dgt )?
    ( [eE] ( [+\-] $exp_sign )? [0-9]+ $exp )?
) %number;

main := ( number '\n' )*;

get's compiled down to

st0:
    if ( ++p == pe )
        goto out0;
    if ( 48 <= (*p) && (*p) <= 57 )
        goto tr0;
    goto st_err;
tr0:
    { printf("DGT: %c\n", (*p)); }
st1:
    if ( ++p == pe )
        goto out1;
    switch ( (*p) ) {
        case 10: goto tr5;
        case 46: goto tr7;
        case 69: goto st4;
        case 101: goto st4;
    }
    if ( 48 <= (*p) && (*p) <= 57 )
        goto tr0;
    goto st_err;
bradgonesurfing