I can't get the $ (dollar-sign) to work as documented in C++11 regular expressions. This is with ECMAScript syntax (the default).
我不能让$(美元符号)像C ++ 11正则表达式中记录的那样工作。这是ECMAScript语法(默认)。
Example (regex.cc):
#include <iostream>
#include <regex>
int main() {
if ( std::regex_search("one\ntwo", std::regex{"one$"}) ) {
std::cout << "Should match, doesn't." << std::endl;
}
if ( std::regex_search("one\ntwo", std::regex{"two$"}
, std::regex_constants::match_not_eol) ) {
std::cout << "Shouldn't match, does." << std::endl;
}
return 0;
}
Expected output: Should match, doesn't.
预期输出:应该匹配,不匹配。
Actual output: Shouldn't match, does.
实际输出:不应该匹配,确实如此。
From http://www.cplusplus.com/reference/regex/ECMAScript/:
$
- End of line - Either it is the end of the target sequence, or precedes a line terminator.$ - 行结束 - 它是目标序列的末尾,或者在行终止符之前。
From http://www.cplusplus.com/reference/regex/regex_search/:
match_not_eol
- Not End-Of-Line - The last character is not considered an end of line ("$"
does not match).match_not_eol - Not End-Of-Line - 最后一个字符不被视为行尾(“$”不匹配)。
Tested with Clang 3.3 and 3.4 on FreeBSD 10:
在FreeBSD 10上使用Clang 3.3和3.4进行测试:
clang++ -std=c++11 -stdlib=libc++ -o regex regex.cc && ./regex
What am I missing?
我错过了什么?
1 个解决方案
#1
5
Looks like you stumbled on LWG issue 2343
看起来你偶然发现LWG问题2343
To quote,
If Multiline is true, $ matches just before LineTerminator.
如果Multiline为true,则$恰好在LineTerminator之前匹配。
If Multiline is false, $ does not match just before LineTerminator.
如果Multiline为false,则$恰好在LineTerminator之前。
[,,,]
Multiline of the existing implementations are as follows:
现有实现的多行如下:
Multiline=false:
libstdc++ r206594
libc++ r199174
Multiline=true:
Visual Studio Express 2013
Visual Studio Express 2013
boost 1.55
Note: using the current SVN version of libc++
, your first test IS actually matched, so looks like this LWG issue is going to be resolved in Multiline's favor
注意:使用当前的SVN版本的libc ++,你的第一个测试IS实际上是匹配的,所以看起来这个LWG问题将在Multiline中得到解决
The second issue (match_not_eol
ignored) looks like a fairly straightforward implementation bug. Boost.regex doesn't match that test case.
第二个问题(match_not_eol被忽略)看起来像一个相当简单的实现错误。 Boost.regex与该测试用例不匹配。
#1
5
Looks like you stumbled on LWG issue 2343
看起来你偶然发现LWG问题2343
To quote,
If Multiline is true, $ matches just before LineTerminator.
如果Multiline为true,则$恰好在LineTerminator之前匹配。
If Multiline is false, $ does not match just before LineTerminator.
如果Multiline为false,则$恰好在LineTerminator之前。
[,,,]
Multiline of the existing implementations are as follows:
现有实现的多行如下:
Multiline=false:
libstdc++ r206594
libc++ r199174
Multiline=true:
Visual Studio Express 2013
Visual Studio Express 2013
boost 1.55
Note: using the current SVN version of libc++
, your first test IS actually matched, so looks like this LWG issue is going to be resolved in Multiline's favor
注意:使用当前的SVN版本的libc ++,你的第一个测试IS实际上是匹配的,所以看起来这个LWG问题将在Multiline中得到解决
The second issue (match_not_eol
ignored) looks like a fairly straightforward implementation bug. Boost.regex doesn't match that test case.
第二个问题(match_not_eol被忽略)看起来像一个相当简单的实现错误。 Boost.regex与该测试用例不匹配。