C++ TR1 正则表达式 - 多行选项
我认为 $ 表示字符串的结尾。然而,下面的代码给出了“testbbbccc”作为结果,这对我来说非常令人惊讶......这意味着 $ 实际上匹配行尾,而不是整个字符串的结尾。
#include <iostream>
#include <regex>
using namespace std;
int main()
{
tr1::regex r("aaa([^]*?)(ogr|$)");
string test("bbbaaatestbbbccc\nddd");
vector<int> captures;
captures.push_back(1);
const std::tr1::sregex_token_iterator end;
for (std::tr1::sregex_token_iterator iter(test.begin(), test.end(), r, captures); iter != end; )
{
string& t1 = iter->str();
iter++;
cout << t1;
}
}
我一直在尝试找到一个“多线”开关(实际上可以在 PCRE 中轻松找到),但没有成功......有人可以指出我正确的方向吗?
问候, RP
I thought that $ indicates the end of string. However, the following piece of code gives "testbbbccc" as a result, which is quite astonishing to me... This means that $ actually matches end of line, not end of the whole string.
#include <iostream>
#include <regex>
using namespace std;
int main()
{
tr1::regex r("aaa([^]*?)(ogr|$)");
string test("bbbaaatestbbbccc\nddd");
vector<int> captures;
captures.push_back(1);
const std::tr1::sregex_token_iterator end;
for (std::tr1::sregex_token_iterator iter(test.begin(), test.end(), r, captures); iter != end; )
{
string& t1 = iter->str();
iter++;
cout << t1;
}
}
I have been trying to find a "multiline" switch (which actually can be easily found in PCRE), but without success... Can someone point me to the right direction?
Regards,
R.P.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
由于为 tr1 选择了 Boost::Regex,请尝试以下操作:
来自 Boost::Regex
所以你观察到的行为是正确的。
来自: Boost 正则表达式 还有:
我希望有所帮助。
As Boost::Regex was selected for tr1, try the following:
From Boost::Regex
So the behavior you observed is correct.
From: Boost Regex as well:
I hope that helps.
TR1 正则表达式中没有多行开关。它并不完全相同,但您可以获得匹配所有内容的相同功能:
这会非贪婪匹配每个字符,包括换行符和回车符。
注意:如果您的模式是代码中的 C++ 字符串,请记住像
'\\'
一样转义反斜杠'\'
。注2:如果不想捕获匹配的内容,请在左括号后添加“?:”:
There is no multiline switch in TR1 regexs. It's not exactly the same, but you could get the same functionality matching everything:
This matches non-greedily every character, including new line and carriage return.
Note: Remember to escape the backslashes
'\'
like this'\\'
if your pattern is a C++ string in code.Note 2: If you don't want to capture the matched contents, append '?:' to the opening bracket: