理解 C++11 中的正则表达式
Comprehending regex in C++11
我正在尝试学习 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。代码:2
表达式包含无效的转义字符或尾随转义。
我做错了什么?
更新
GCC 版本 4.8.220131212 (Red Hat 4.8.2-7) (GCC)
CLANG 版本 3.3 (标签/RELEASE_33/最终版)
libstdc++ 版本 4.8.2
溶液
井。我正在阅读"C++编程语言",并想尝试使用std::regex的东西。所以我想解决方案是等待 gcc-4.9。
我赞扬EagleV_Attnam指出了代码中的其他错误。
两件事:
- 字符串
"10;20;30;40;"
仅在match_regex
调用中定义。与cmatch
相反,smatch
期望字符串(如string()
创建的字符串)在您想要访问它时仍然有效。 - 您当前的正则表达式不匹配(至少在我的系统上不匹配)。它尝试匹配整个字符串。在末尾添加一个.*(并开始,但这在您的情况下不是必需的)应该可以修复它,就像让整个事情重复(
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在这方面是正确的 - 但尝试一下应该不会有什么坏处。
正则表达式的一个问题是你没有转义 \,并且 \d 在字符串的上下文中不是有效的转义序列。我不确定您是否可以在字符串上使用 R 标识符,但它对我来说是未定义的。
上次我检查时,GCC 的正则表达式也不完整。因此,您可能会被迫使用加速正则表达式。
regex rx( "\d*;" ); //regexp, must escape ''
string input = "10;20;30;40;";
smatch m;
if( regex_search( input, m, rx ) )
{
cout << m[0] << endl;
}
相关文章:
- 如何在 c++11 中使用正则表达式找到确切的子字符串?
- 如何将Javascript正则表达式转换为C++11正则表达式
- C++11 (MSVS2012) 正则表达式在多行 std::string 中查找文件名
- C 11正则表达式和字符串U8前缀
- C++11 正则表达式匹配不以句点结尾的完整单词
- 有条件地忽略 c++11 正则表达式中的大小写
- C++11 中的默认正则表达式引擎是什么?
- 为什么 pcre 正则表达式比 c++11 正则表达式快得多
- 在 C++11 下使用正则表达式
- C++11 正则表达式行尾不匹配
- C++11正则表达式:在替换字符串中捕获组后的数字
- 为什么 C++11 正则表达式(libc ++ 实现)如此缓慢?
- C++11 CMake:正则表达式失败
- 如何获得与 C++11 或 Boost 匹配的正则表达式长度
- Regex分组与C++11正则表达式库匹配
- 使用 libc++ 正则表达式库匹配"beginning-of-line" (C++11)
- C++11 中的正则表达式
- C++11正则表达式不匹配任何内容
- 将 C++11 正则表达式与 gcc 4.8.2 一起使用时会出现奇怪的结果(但适用于 Boost 正则表达式)
- C++11 正则表达式与字符串不匹配