C++ TR1 正则表达式 - 多行选项

标签 c++ regex 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 &lt;&lt; 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/

相关文章:

c# - 用投影搭建互动楼层?

c++ - 为什么在 C++/CLI 中,限定的、前向声明的 native 类名称解析为不同的托管类名称?

python - 如何使用正则表达式从字符串中提取前两个字符

android - 标准C++库的跨平台程度如何?

c++ - 抽象类使用shared_ptr时如何在nm或objdump中找到函数符号?

regex - AWK - 从文件导入 IF 条件

java - 正则表达式拆分字符串

c++ - 从 std::vector c++ 调用 std::function 时崩溃

c++ - 在对 tr1::bind() 的新调用中嵌套来自 tr1::bind() 的 tr1::bind<> 对象

c++ - 在编译时检查 tr1 数组的大小