tags:

views:

183

answers:

4

I'm looking for a regular expression (or something else) library for C++ that would allow me to specify a number of patterns, run on a string and return the matching locations of all patterns.

For example: Patterns {"abcd", "abcd"} String {"abcd abce abcd"}

Result: abcd matches: 0-3, 11-14 abce matches: 5-9

Anyone know of a such a library?

A: 

boost::regex

fco.javier.sanz
+2  A: 

I recommend boost::xpressive http://www.boost.org/doc/libs/1_39_0/doc/html/xpressive.html.

One of possible solution:

string text = "abcd abce abcd";

static const sregex abcd = as_xpr("abcd");  // static - faster
sregex abce = sregex::compile( "abce" )  // compiled 
sregex all = *(keep(abcd) | keep(abce));

smatch what;

if( regex_match( text, what, all ) ) 
{
    smatch::nested_results_type::const_iterator begin = what.nested_results().begin();
    smatch::nested_results_type::const_iterator end = what.nested_results().end();

    for(;it != end; it++)
    {
        if(it->regex_id() == abcd.regex_id())
        {
            // you match abcd
            // use it->begin() and it->end()
            // or it->position() and it->length()
            continue;
        }
        if(it->regex_id() == abce.regex_id())
        {
            // you match abcd...
            continue;
        };

}

I think is not best solution, you could check “Semantic Actions and User-Defined Assertions” in documentation.

lionbest
A: 

Regular Expressions are part of the standard extension tr1 and implemented in a number of standard libraries (i.e. dinkumware)

I think that its very straightforward to write the surrounding code yourself.

RED SOFT ADAIR
A: 

Doesn't it work with an simple or?

"abcd|abcd"

which is a valid regular expression.

Totonga
A simple or will find me only "abcd" OR "abce" and it's locations.So in the above case, it will only find the locations of abcd.
HellSpam
No, or (alteration) of abcd|abce should find all locations that abcd or abce match.
Avi