0

私はC++に取り組んでいます、

特定の文字列で特定の正規表現を検索する必要があります。それを行うためのポインタを教えてください。boost::regex ライブラリを使用しようとしました。

以下は正規表現です: 検索する正規表現:"get*"

上記の式では、次の異なる文字列を検索する必要があります。

1.    "com::sun::star:getMethodName"
2.    "com:sun:star::SetStatus"
3.    "com::sun::star::getMessage"

したがって、上記のケースでは、最初の文字列に対して true を取得し、2 番目の文字列に対して false を取得し、3 番目の文字列に対して true を取得する必要があります。前もって感謝します。

4

1 に答える 1

2
boost::regex re("get.+");

例。

#include <iostream>
#include <string>
#include <boost/regex.hpp>
#include <vector>
#include <algorithm>

int main()
{
   std::vector<std::string> vec = 
   {
      "com::sun::star:getMethodName",
      "com:sun:star::SetStatus",
      "com::sun::star::getMessage"
   };
   boost::regex re("get.+");
   std::for_each(vec.begin(), vec.end(), [&re](const std::string& s)
   {
      boost::smatch match;
      if (boost::regex_search(s, match, re))
      {
         std::cout << "Matched" << std::endl;
         std::cout << match << std::endl;
      }
   });
}

http://liveworkspace.org/code/7d47ad340c497f7107f0890b62ffa609

于 2012-08-06T06:00:37.643 に答える