2
votes

using google re2 library for regex i haven't found a way to parse results, anywhere!

this is a short example

bool b_matches ;
string s_teststr = " aaaaa flickr bbbb";
RE2 re("(?P<flickr>flickr)|(?P<flixster>flixster)");
assert(re.ok()); // compiled; if not, see re.error();
b_matches = RE2::FullMatch(s_teststr, re);

  b_matches = RE2::FullMatch(s_teststr, re);

// then,
re.NumberOfCapturingGroups() //-> always give me 2

 re.CapturingGroupNames(); //-> give me a map with id -> name (with 2 elements)

re.NamedCapturingGroups() //-> give me a map with name -> id (with 2 elements)

what i have to do to know that only flickr has been matched ?

thank you,

Francesco

--- after some more testing i didn't have found a soltuion for the namedcapture, only way thing i have found working give me the extracted text and is this.

string s_teststr = "aaa  hello. crazy world bbb";
std::string word[margc];
RE2::Arg margv[margc];
RE2::Arg * margs[margc];
int match;
int i;

    for (i = 0; i < margc; i++) {
        margv[i] = &word[i];
        margs[i] = &margv[i];
    }
   string s_rematch = "((?P<a>hello\\.)(.*)(world))|(world)";
  match = RE2::PartialMatchN(s_teststr.c_str(), s_rematch.c_str(), margs, margc);
cout << "found res = " << match << endl;
  for (int i = 0; i < margc; i++) {
        cout << "arg[" << i << "] = " << word[i] << endl;
    }

-------- this will give me in output:

found res = 1 arg[0] = hello. crazy world arg[1] = hello. arg[2] = crazy arg[3] = world arg[4] =

to test with the second part of the string matching...

string s_rematch = "((?P<a>hello\\.d)(.*)(world))|(world)";

--- i get as output:

foudn res = 1 arg[0] = arg[1] = arg[2] = arg[3] = arg[4] = world

my problem is taht the name capture --> a <--- never come out and the output should be cleared (lowercase in case of insensitive match, removed from added compatibily chars,.. ) and processed again against a map because i don't have the named capture which give me the key instead of the value for this preg

1
got it! the only way i've found to get my named capture is to parse the resutls args and when string length is > 0 then search for the arg id into ---------------------------------------------------- const map<int, string>& m_RE_namedmap2 = re_compiled.CapturingGroupNames(); // -- id => name -------------------------------------- this give me the keyword corresponding to the id found , than i have to create a new map with key (found searching for the id into the map of CapturingGroupNames and with the value which is the string with length > 0Francesco

1 Answers

0
votes

You can pass in a string to be populated upon success. For example:

std::string matchedValue;

if (RE2::FullMatch(s_teststr, re, &matchedValue))
{
    if (matchedValue.empty())
    {
        //not flickr
    }
}
else
{
    // matchedValue.empty() == true
}