C++ 如何使用RE2查找匹配字符串的偏移量?
是一个现代正则表达式引擎,可从谷歌获得。我想在一个目前正在使用gnuregex的程序中使用RE2。我遇到的问题是找出匹配的。RE2返回的是匹配的字符串。我需要知道匹配项的偏移量。我现在的计划是使用什么Re2返回,然后在C++字符串上使用<代码>查找< /C>。但这似乎是浪费。我看了RE2手册,不知道怎么做。有什么想法吗?将结果存储在C++ 如何使用RE2查找匹配字符串的偏移量?,c++,regex,re2,C++,Regex,Re2,是一个现代正则表达式引擎,可从谷歌获得。我想在一个目前正在使用gnuregex的程序中使用RE2。我遇到的问题是找出匹配的。RE2返回的是匹配的字符串。我需要知道匹配项的偏移量。我现在的计划是使用什么Re2返回,然后在C++字符串上使用查找< /C>。但这似乎是浪费。我看了RE2手册,不知道怎么做。有什么想法吗?将结果存储在re2::StringPiece而不是std::string中。.data()的值将指向原始字符串 以这个项目为例。 在每个测试中,result.data()是指向原始con
re2::StringPiece
而不是std::string
中。.data()
的值将指向原始字符串
以这个项目为例。
在每个测试中,result.data()
是指向原始const char*
或std::string
的指针
#include <re2/re2.h>
#include <iostream>
int main(void) {
{ // Try it once with character pointers
const char *text[] = { "Once", "in", "Persia", "reigned", "a", "king" };
for(int i = 0; i < 6; i++) {
re2::StringPiece result;
if(RE2::PartialMatch(text[i], "([aeiou])", &result))
std::cout << "First lower-case vowel at " << result.data() - text[i] << "\n";
else
std::cout << "No lower-case vowel\n";
}
}
{ // Try it once with std::string
std::string text[] = { "While", "I", "pondered,", "weak", "and", "weary" };
for(int i = 0; i < 6; i++) {
re2::StringPiece result;
if(RE2::PartialMatch(text[i], "([aeiou])", &result))
std::cout << "First lower-case vowel at " << result.data() - text[i].data() << "\n";
else
std::cout << "No lower-case vowel\n";
}
}
}
#包括
#包括
内部主(空){
{//使用字符指针尝试一次
const char*text[]={“曾经”、“在”、“波斯”、“统治”、“a”、“国王”};
对于(int i=0;i<6;i++){
re2::StringPiece结果;
if(RE2::PartialMatch(文本[i],“([aeiou])”,&result))
std::这正是我需要的。谢谢。这在文档中吗?我找不到。我没有找到明确列出的答案,但我能够从中推断出来。我需要做完全相同的事情,只是我不能修改正则表达式以添加捕获括号。在这种情况下,我如何知道部分匹配的位置?