如何匹配 Poco::RegularExpression C++ 中的“\n”?

How to match "\n" in Poco::RegularExpression C++?

我当前的代码是:

#include <iostream>
#include <Poco/Foundation.h>
#include <Poco/RegularExpression.h>

int main()
{
    Poco::RegularExpression regex("[A-Z]+\s+[A-Z]+");
    Poco::RegularExpression::MatchVec mvec;
    constad std::string astring = "ABC\nDEFG";

    int matches = regex.match(astring,0,mvec);

    std::cout << "Hello World\n";

    return 0;
 }

'\n' 在我尝试匹配的字符串中的位置可以是单个 space、多个 space 或新行(因此我使用白色space 元字符)。

返回的匹配项数为零。是否有我需要设置的标志或其他东西?

这应该有效

Poco::RegularExpression s ("\s"); // White char
Poco::RegularExpression n ("\n"); // New line
Poco::RegularExpression r ("\r"); // Carrige return
Poco::RegularExpression t ("\t"); // Tabulator

问题出在您的正则表达式中的转义序列。

在这种情况下,您希望使用标记 \s 在字符串 astring 中添加反斜杠 (\),但在 C/C++ 或 Java 必须写成double \。因此,要解决您的问题,您必须添加另一个反斜杠:

Poco::RegularExpression regex("[A-Z]+\s+[A-Z]+");

在这里你可以找到参考:

http://en.cppreference.com/w/cpp/language/escape