0

I'm working on C++,

I need to search for a given regular expression in given string. Please provide me the pointer to do it. I tried to use boost::regex library.

Following is the regular expression: regular expression to search : "get*"

And above expression i have to search in following different strings: e.g.

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

so i above case i should get true for first string false for second and again true for third one. Thanks in advance.

manlio
  • 18,345
  • 14
  • 76
  • 126
BSalunke
  • 11,499
  • 8
  • 34
  • 68

1 Answers1

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

example.

#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

ForEveR
  • 55,233
  • 2
  • 119
  • 133
  • Thanks for your inputs. It is working but i have one query that can't we use linux wild characters? Ans what are the characters that we can use as "." is used above? – BSalunke Aug 06 '12 at 06:14
  • 2
    @BSalunke ehm, how about read documentation? http://www.boost.org/doc/libs/1_50_0/libs/regex/doc/html/boost_regex/syntax/perl_syntax.html . You can use this regex as well boost::regex re("get\\w+"); – ForEveR Aug 06 '12 at 06:19
  • Yes,, i gone through that Perl Regular Expression Syntax but i need to parse the regular expression in c++. So what are the wild characters should be used? – BSalunke Aug 06 '12 at 08:05
  • @BSalunke Can't understood your question... You can use '.' or '\\w' characters for your strings. – ForEveR Aug 06 '12 at 08:21