C++ 避免正则表达式的贪婪

C++ 避免正则表达式的贪婪,c++,C++,基本正则表达式问题 默认情况下,正则表达式看起来是贪婪的。例如,以下代码: #include <regex> #include <iostream> int main() { const std::string t = "*1 abc"; std::smatch match; std::regex rgxx("\\*(\\d+?)\\s+(.+?)$"); bool matched1 = std::regex_search(t.begin(), t.en

基本正则表达式问题

默认情况下,正则表达式看起来是贪婪的。例如,以下代码:

#include <regex>
#include <iostream>

int main() {
  const std::string t = "*1 abc";
  std::smatch match;
  std::regex rgxx("\\*(\\d+?)\\s+(.+?)$");
  bool matched1 = std::regex_search(t.begin(), t.end(), match, rgxx);
  std::cout << "Matched size " << match.size() << std::endl;

  for(int i = 0 ; i < match.size(); ++i) {
    std::cout << i << " match " << match[i] << std::endl;
  }
}
作为一名通用正则表达式编写人员,我只希望

1 match 1
2 match abc
来吧。我认为第一场比赛的到来是因为regex的贪婪。它是如何避免的?

From:
匹配[0]
不是贪婪求值的结果,而是整个匹配的范围。匹配元素
[1,n)
是捕获组

以下是比赛结果的说明:

regex     "hello ([\\w]+)"

string   = "Oh, hello John!"
match[0] =     "hello John"   // matches the whole regex above
match[1] =           "John"   // the first capture group

您只有一个匹配项。该匹配项有2个“标记子表达式”,因为这是正则表达式指定的。您没有该正则表达式的多个匹配项

m.size()
:标记的子表达式数加1,即
1+rgxx.mark\u count()


如果您要查找多个匹配项,请使用

如何避免它?我不确定您的意思,
regex\u search
旨在查找匹配项,而
match[0]
是否匹配。如果您有一个只匹配字符串中间部分的正则表达式,可能会更清楚。但是,如果您不关心匹配,只需要捕获组,请忽略它。
regex     "hello ([\\w]+)"

string   = "Oh, hello John!"
match[0] =     "hello John"   // matches the whole regex above
match[1] =           "John"   // the first capture group