Boost RegEx:具体问题

发布于 2024-11-29 02:49:39 字数 2063 浏览 0 评论 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;
}

似乎有效。请尽管这样我可以接受您最喜欢的网站上的答案,并给新手一些指示 =)。

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 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 =).

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

物价感观 2024-12-06 02:49:39

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

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

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+)"
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文