C ++ TR1正则表达式 - 多行选项 [英] C++ TR1 regex - multiline option
问题描述
我认为$表示字符串的结束。然而,下面的代码段给出了testbbbccc作为结果,这对我来说是非常令人惊讶的...这意味着$实际匹配行结束,而不是整个字符串的结尾。
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;
}
}
我一直在试图找到一个多线 (实际上可以在PCRE中轻松找到),但没有成功...有人可以指向正确的方向吗?
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?
注意,
RP
Regards, R.P.
推荐答案
由于tr1选择了Boost :: Regex,请尝试以下操作:
As Boost::Regex was selected for tr1, try the following:
锚定:
Anchors:
一个行的开始
用作表达式的第一个
字符或
子表达式的第一个字符。
A '^' character shall match the start of a line when used as the first character of an expression, or the first character of a sub-expression.
当用作表达式的最后一个字符
或子表达式的最后一个
字符时,'$'字符将匹配
a行的结尾。
A '$' character shall match the end of a line when used as the last character of an expression, or the last character of a sub-expression.
因此,您观察到的行为是正确的。
So the behavior you observed is correct.
From: Boost Regex :
\\ A
在缓冲区开始处匹配
(与\\`
)。
\\ z
在
匹配仅缓冲区的结尾与
\\'
相同。
\\ Z
匹配可选序列
在缓冲区结尾处的换行符:
相当于正则表达式
\\ n * \\ z
\A
Matches at the start of a buffer only (the same as\`
).
\z
Matches at the end of a buffer only (the same as\'
).
\Z
Matches an optional sequence of newlines at the end of a buffer: equivalent to the regular expression\n*\z
我希望有帮助。
这篇关于C ++ TR1正则表达式 - 多行选项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!