代码之家  ›  专栏  ›  技术社区  ›  SunDontShine

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

  •  0
  • SunDontShine  · 技术社区  · 8 年前

    #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;
     }
    

    返回的匹配数为零。有我需要挂的旗子吗?

    2 回复  |  直到 8 年前
        1
  •  0
  •   Jorge Omar Medra    8 年前

    问题是正则表达式中的scape序列。

    在这种情况下,您需要添加反斜杠( \ astring ,使用令牌 \s ,但在C/C++或Java中,它必须写成双精度 \\ 因此,要解决您的问题,您必须添加另一个反斜杠:

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

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

        2
  •  0
  •   kocica    8 年前

    这应该行得通

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