gpt4 book ai didi

c++ - 理解 C++11 中的正则表达式

转载 作者:塔克拉玛干 更新时间:2023-11-03 02:24:42 25 4
gpt4 key购买 nike

我正在尝试学习 C++11 中的正则表达式。一定是做错了什么,因为括号或转义序列似乎都不起作用。

这是我的代码:

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

using namespace std;

int main()
{
try
{
cout << R"(\d*(\.\d*)?;)" << endl << endl;

regex rx{ R"(\d*(\.\d*)?;)", regex_constants::ECMAScript };
smatch m;

if( regex_match( string( "10;20;30;40;" ), m, rx ) )
{
cout << m[0];
}
}
catch( const regex_error &e )
{
cerr << e.what() << ". Code: " << e.code() << endl;

switch( e.code() )
{
case regex_constants::error_collate:
cerr << "The expression contained an invalid collating element name.";
break;
case regex_constants::error_ctype:
cerr << "The expression contained an invalid character class name.";
break;
case regex_constants::error_escape:
cerr << "The expression contained an invalid escaped character, or a trailing escape.";
break;
case regex_constants::error_backref:
cerr << "The expression contained an invalid back reference.";
break;
case regex_constants::error_brack:
cerr << "The expression contained mismatched brackets ([ and ]).";
break;
case regex_constants::error_paren:
cerr << "The expression contained mismatched parentheses (( and )).";
break;
case regex_constants::error_brace:
cerr << "The expression contained mismatched braces ({ and }).";
break;
case regex_constants::error_badbrace:
cerr << "The expression contained an invalid range between braces ({ and }).";
break;
case regex_constants::error_range:
cerr << "The expression contained an invalid character range.";
break;
case regex_constants::error_space:
cerr << "There was insufficient memory to convert the expression into a finite state machine.";
break;
case regex_constants::error_badrepeat:
cerr << "The expression contained a repeat specifier (one of *?+{) that was not preceded by a valid regular expression.";
break;
case regex_constants::error_complexity:
cerr << "The complexity of an attempted match against a regular expression exceeded a pre-set level.";
break;
case regex_constants::error_stack:
cerr << "There was insufficient memory to determine whether the regular expression could match the specified character sequence.";
break;
default:
cerr << "Undefined.";
break;

}

cerr << endl;
}

return 0;
}

输出:

\d*(.\d*)?;

regex_error. Code: 2

The expression contained an invalid escaped character, or a trailing escape.

我做错了什么?

更新

gcc 版本 4.8.2 20131212(红帽 4.8.2-7)(海湾合作委员会)

clang 版本 3.3(标签/RELEASE_33/final)

libstdc++ 版本 4.8.2

解决方案

嗯。我正在阅读“C++ 编程语言”并想尝试使用 std::regex 内容。所以我想解决方案是等待 gcc-4.9。

我感谢 EagleV_Attnam 指出了我代码中的其他错误。

最佳答案

两件事:

  1. 您的字符串 "10;20;30;40;" 仅在 match_regex 调用中定义。 smatchcmatch 不同,它期望字符串(如 string() 创建的字符串)在您想要访问它的时间。
  2. 您当前的正则表达式不匹配(至少在我的系统上不匹配)。它试图匹配整个字符串。在末尾添加一个 .* (并开始,但在您的情况下这不是必需的)应该修复它,就像让整个事情重复一样(使用 R"((stuff)*)")

工作代码(但无法在 gcc 上尝试):

regex rx{ R"(\d*(\.\d*)?;.*)", regex_constants::ECMAScript };
smatch m;
string s("10;20;30;40;");
if (regex_match(s, m, rx))
{
cout << m[0];
}

不知道这是否会解决您的特定错误 - 恐怕 KitsuneYMG 在这方面是正确的 - 但尝试应该不会有什么坏处。

关于c++ - 理解 C++11 中的正则表达式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22840771/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com