开发者

Boost RegEx: Specific Question

I am trying to use this expression:

Expression:  "\w{1,}\s*?\-\-(\>)?\s*?\w{1,}"

Keep in mind I am escaping the \ with a second \ in my code.

When searching in the strings below. I think I am close, but no cigar. I want the expression above to be able to find matches开发者_JAVA百科 in the text below. Where am I going wrong?

Text:        "AB --> CD"
Text:        "AB --> Z"
Text:        "A --> 123d"
etc.

Resources Used:

  1. http://www.solarix.ru/for_developers/api/regex-en.html

  2. http://www.boost.org/doc/libs/1_47_0/libs/regex/doc/html/boost_regex/introduction_and_overview.html

  3. http://www.regular-expressions.info/reference.html


UPDATE

The comment helped me. I would still like to see people post on my thread, for record keeping purposes, regex sites that have helped them master regex. Anyways my code (mostly copied from the boost website) is.

/* All captures from a regular expression */
#include <boost/regex.hpp>
#include <iostream>

/* Compiled with g++ -o regex_tut -lboost_regex -Wall ./regex_tut.cpp */

void print_captures(const std::string& regx, const std::string& text)
{
   boost::regex e(regx);
   boost::smatch what;
   std::cout << "Expression:  \"" << regx << "\"\n";
   std::cout << "Text:        \"" << text << "\"\n";
   if(boost::regex_match(text, what, e, boost::match_extra))
   {
      unsigned i;
      std::cout << "** Match found **\n   Sub-Expressions:\n";
      for(i = 0; i < what.size(); ++i) {
         std::cout << "      $" << i << " = \"" << what[i] << "\"\n";
      }
   }
   else
   {
      std::cout << "** No Match found **\n";
   }
}

int main(int argc, char* argv[ ])
{
   print_captures("^\\w+\\s*-->?\\s*\\w+\\s*(\\(\\d+\\))?", "AB --> CD (12)" );
   return 0;
}

Seems to work. Please though so I can accept an answer post your favorite site up and give a newb a few pointers =).


Not sure if i understood your question correctly, but if you want your regex to match for example AB and CD in "AB --> CD" you can use the following regex:

Expression:  "(\w+)\s*-->?\s*(\w+)"
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜