0

我正在尝试使用这个表达式:

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

请记住,我在代码中用第二个 \ 转义了 \。

在下面的字符串中搜索时。我想我很接近,但没有雪茄。我希望上面的表达式能够在下面的文本中找到匹配项。我哪里错了?

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

使用的资源:

  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


更新

评论帮助了我。我仍然希望看到人们在我的线程上发帖,出于记录目的,帮助他们掌握正则表达式的正则表达式站点。无论如何,我的代码(主要是从 boost 网站复制的)是。

/* 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;
}

似乎工作。请尽管如此,我可以接受将您最喜欢的网站发布的答案,并给新手一些指示=)。

4

1 回答 1

1

不确定我是否正确理解了您的问题,但是如果您希望您的正则表达式匹配例如并且AB您可以使用以下正则表达式:CD"AB --> CD"

Expression:  "(\w+)\s*-->?\s*(\w+)"
于 2011-08-12T05:09:57.247 回答