2

使用 google re2 库进行正则表达式我还没有找到一种方法来解析结果,任何地方!

这是一个简短的例子

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)

我必须做什么才能知道只有 flickr 被匹配?

谢谢你,

弗朗切斯科

--- 经过更多测试后,我没有找到命名捕获的解决方案,我发现工作的唯一方法是给我提取的文本,就是这样。

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

-------- 这会给我输出:

发现 res = 1 arg[0] = 你好。疯狂的世界 arg[1] = 你好。arg[2] = 疯狂 arg[3] = 世界 arg[4] =

用字符串匹配的第二部分进行测试...

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

---我得到输出:

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

我的问题是名称捕获-> a <--- 永远不会出现,并且应该清除输出(在不敏感匹配的情况下小写,从添加的兼容字符中删除,..)并再次针对地图进行处理,因为我不'没有命名的捕获,它给了我密钥而不是这个 preg 的值

4

1 回答 1

0

您可以传入一个字符串以在成功时填充。例如:

std::string matchedValue;

if (RE2::FullMatch(s_teststr, re, &matchedValue))
{
    if (matchedValue.empty())
    {
        //not flickr
    }
}
else
{
    // matchedValue.empty() == true
}
于 2011-04-26T14:33:23.853 回答