Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ std::match_results::size返回什么?_C++_Regex_C++11 - Fatal编程技术网

C++ std::match_results::size返回什么?

C++ std::match_results::size返回什么?,c++,regex,c++11,C++,Regex,C++11,我对以下C++11代码有点困惑: #include <iostream> #include <string> #include <regex> int main() { std::string haystack("abcdefabcghiabc"); std::regex needle("abc"); std::smatch matches; std::regex_search(haystack, matches, needl

我对以下C++11代码有点困惑:

#include <iostream>
#include <string>
#include <regex>

int main()
{
    std::string haystack("abcdefabcghiabc");
    std::regex needle("abc");
    std::smatch matches;
    std::regex_search(haystack, matches, needle);
    std::cout << matches.size() << std::endl;
}
#包括
#包括
#包括
int main()
{
标准:字符串草垛(“abcdefabcghiabc”);
标准::regex针(“abc”);
std::smatch匹配;
标准::正则搜索(草堆、火柴、针);

std::cout您得到
1
,因为只返回1个匹配项,
size()
将返回捕获组数+整个匹配值

您的
匹配项是…:

对象的类型(如cmatch或smatch),该类型由此函数填充有关匹配结果和找到的任何子匹配的信息

如果[regex搜索]成功,则它不是空的,并且包含一系列子匹配对象:第一个元素对应于整个匹配,如果regex表达式包含要匹配的子表达式(即括号分隔的组),其对应的子匹配项将作为连续的子匹配元素存储在match_results对象中

以下代码将查找多个匹配项:

#include <string>
#include <iostream>
#include <regex>
using namespace std;
int main() {
  string str("abcdefabcghiabc");
  int i = 0;
  regex rgx1("abc");
  smatch smtch;
  while (regex_search(str, smtch, rgx1)) {
        std::cout << i << ": " << smtch[0] << std::endl;
        i += 1;
        str = smtch.suffix().str();
  }
  return 0;
}
看,回来

Match value: abc at Position 0
    Capture: c at Position 2
Match value: abc at Position 6
    Capture: c at Position 8
Match value: abc at Position 12
    Capture: c at Position 14

您缺少的是,
匹配
为每个捕获组填充了一个条目(包括作为第0次捕获的整个匹配子字符串)

如果你写信

std::regex needle("a(b)c");

然后你会得到
匹配项。size()==2
,带有
匹配项[0]==“abc”
,和
匹配项[1]==“b”
编辑:一些人否决了这个答案。这可能是因为各种原因,但如果是因为它不适用于我批评的答案(没有人留下评论来解释这个决定),他们应该注意到,W.Stribizew在我写这篇文章两个月后改变了代码,直到今天,2021-01-18,我才意识到这一点。答案的其余部分与我第一次写这篇文章时没有变化

@Stribizev的解决方案对于正常的正则表达式具有二次最坏情况复杂性。对于不正常的正则表达式(例如“y*”),它不会终止。在某些应用程序中,这些问题可能会等待发生。以下是一个固定版本:

string str("abcdefabcghiabc");
int i = 0;
regex rgx1("abc");
smatch smtch;
auto beg = str.cbegin();
while (regex_search(beg, str.cend(), smtch, rgx1)) {
    std::cout << i << ": " << smtch[0] << std::endl;
    i += 1;
    if ( smtch.length(0) > 0 )
        std::advance(beg, smtch.length(0));
    else if ( beg != str.cend() )
        ++beg;
    else
        break;
}

每次搜索只能得到1个匹配项。注意:上面的示例将删除
str
,因此如果需要保留它,请复制一份。重要的一点是获取
smatch.suffix().str()
它返回匹配后的文本。
while
循环在剩余字符串上重复匹配,直到找不到更多匹配。是的,是的,为了保留
str
,只需使用它的副本。@Morpheu5,如果还有什么不清楚的地方,请告诉我。我已经用另一个
std::sregex\u迭代器更新了答案。
-获取所有(子)匹配及其在输入字符串中的位置的方法。
string str("abcdefabcghiabc");
int i = 0;
regex rgx1("abc");
smatch smtch;
auto beg = str.cbegin();
while (regex_search(beg, str.cend(), smtch, rgx1)) {
    std::cout << i << ": " << smtch[0] << std::endl;
    i += 1;
    if ( smtch.length(0) > 0 )
        std::advance(beg, smtch.length(0));
    else if ( beg != str.cend() )
        ++beg;
    else
        break;
}
for (int j = 0; j < 20; ++j)
    str = str + str;