如何在c++中搜索正则表达式

How to search for a regular expression in c++?

本文关键字:搜索 正则表达式 c++      更新时间:2023-10-16

我正在开发C++,

我需要在给定的字符串中搜索给定的正则表达式。请为我提供指针。我尝试使用boost::regex库。

以下是正则表达式:要搜索的正则表达式:"get*"

在上面的表达式中,我必须在以下不同的字符串中搜索:例如

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

所以我在上面的情况下,第一个字符串应该为true,第二个字符串为false,第三个字符串为true。提前谢谢。

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