我以为 $ 表示字符串的结尾。然而,下面的一段代码给出了“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 中轻松找到),但没有成功......有人能指出我正确的方向吗?
问候, R.P.
最佳答案
由于为 tr1 选择了 Boost::Regex,请尝试以下操作:
来自 Boost::Regex
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 '$' 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.
所以你观察到的行为是正确的。
发件人:Boost Regex还有:
\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 正则表达式 - 多行选项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4408914/