带有 BoostRegex C++ 的正则表达式

发布于 2024-10-20 14:18:40 字数 1733 浏览 2 评论 0原文

您好,我希望获得以下表达式的值: 多边形(100 20, 30 40, 20 10, 21 21) 搜索 POLYGON(100 20, 30 40, 20 10, 21 21)

当我执行以下代码时,我得到以下结果:
多边形(100 20, 30 40, 20 10, 21 21)
结果 = 100 20
r2 = 100
r2 = 20
r2 = , 21 21
r2 = 21
size = 7

我不知道为什么我没有获得中间值...

感谢您的帮助

#include <iostream>
#include <boost/regex.hpp>

using namespace std;

void testMatch(const boost::regex &ex, const string st) {
 cout << "Matching " << st << endl;
 if (boost::regex_match(st, ex)) {
  cout << " matches" << endl;
 }
 else {
  cout << " doesn’t match" << endl;
 }
}

void testSearch(const boost::regex &ex, const string st) {
 cout << "Searching " << st << endl;
 string::const_iterator start, end;
 start = st.begin();
 end = st.end();
 boost::match_results<std::string::const_iterator> what;
 boost::match_flag_type flags = boost::match_default;
 while(boost::regex_search(start, end, what, ex, flags))
 {
  cout << " " << what.str() << endl;
  cout << " result = " << what[1] << endl;
  cout << " r2 = " << what[2] << endl;
cout << " r2 = " << what[3] << endl;
cout << " r2 = " << what[4] << endl;
cout << " r2 = " << what[5] << endl;
cout << "size = " << what.size() << endl;
  start = what[0].second;
 }
}

int main(int argc, char *argv[])
{
 static const boost::regex ex("POLYGON\\(((\\-?\\d+) (\\-?\\d+))(\\, (\\-?\\d+) (\\-?\\d+))*\\)");
 testSearch(ex, "POLYGON(1 2)");
 testSearch(ex, "POLYGON(-1 2, 3 4)");
 testSearch(ex, "POLYGON(100 20, 30 40, 20 10, 21 21)");
 return 0;
}

Hi i wish to get the values of the following expression :
POLYGON(100 20, 30 40, 20 10, 21 21)
Searching POLYGON(100 20, 30 40, 20 10, 21 21)

When i execute the following code i obtains this result :

POLYGON(100 20, 30 40, 20 10, 21 21)

result = 100 20

r2 = 100

r2 = 20

r2 = , 21 21

r2 = 21

size = 7

I don't know why i not obtains the middled values...

Thank for your help

#include <iostream>
#include <boost/regex.hpp>

using namespace std;

void testMatch(const boost::regex &ex, const string st) {
 cout << "Matching " << st << endl;
 if (boost::regex_match(st, ex)) {
  cout << " matches" << endl;
 }
 else {
  cout << " doesn’t match" << endl;
 }
}

void testSearch(const boost::regex &ex, const string st) {
 cout << "Searching " << st << endl;
 string::const_iterator start, end;
 start = st.begin();
 end = st.end();
 boost::match_results<std::string::const_iterator> what;
 boost::match_flag_type flags = boost::match_default;
 while(boost::regex_search(start, end, what, ex, flags))
 {
  cout << " " << what.str() << endl;
  cout << " result = " << what[1] << endl;
  cout << " r2 = " << what[2] << endl;
cout << " r2 = " << what[3] << endl;
cout << " r2 = " << what[4] << endl;
cout << " r2 = " << what[5] << endl;
cout << "size = " << what.size() << endl;
  start = what[0].second;
 }
}

int main(int argc, char *argv[])
{
 static const boost::regex ex("POLYGON\\(((\\-?\\d+) (\\-?\\d+))(\\, (\\-?\\d+) (\\-?\\d+))*\\)");
 testSearch(ex, "POLYGON(1 2)");
 testSearch(ex, "POLYGON(-1 2, 3 4)");
 testSearch(ex, "POLYGON(100 20, 30 40, 20 10, 21 21)");
 return 0;
}

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

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

发布评论

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

评论(2

洒一地阳光 2024-10-27 14:18:40

我不是正则表达式专家,但我读了你的正则表达式,它似乎是正确的。

此论坛帖子似乎在谈论完全相同的事情,其中Boost.Regex 仅返回正则表达式的最后一个结果。显然,默认情况下,Boost 仅跟踪重复匹配的最后一场比赛。但是,有一个实验性功能可以让您更改此设置。 更多信息,位于“重复捕获”下。

不过,还有另外 2 个“解决方案”:

  1. 使用正则表达式来跟踪第一对数字,然后获取删除该数字对的子字符串,并对该子字符串执行另一个正则表达式,直到获得所有输入。

  2. 使用Boost.Spirit,它可能比Boost.Regex更适合解析输入。

I am not a regex expert, but I read your regular expression and it seems to be correct.

This forum post appears to be talking about exactly the same thing, where Boost.Regex only returns the last result of a regular expression. Apparently by default Boost only keeps track of the last match of a repetition of matches. However, there is an experimental feature that allows you to change this. More info here, under "Repeated Captures".

There are 2 other "solutions" though:

  1. Use a regex to track the first pair of numbers, then get the substring with that pair removed and do another regex on that substring, until you've got all input.

  2. Use Boost.Spirit, it's probably more suited for parsing input than Boost.Regex.

岁月静好 2024-10-27 14:18:40

我已经从 IRC 频道得到了结果。
正则表达式是:

static const boost::regex ex("[\\d\\s]+");
static const boost::regex ex("[\\-\\d\\s]+");

I have got the result from IRC channel.
The regular expression is :

static const boost::regex ex("[\\d\\s]+");
static const boost::regex ex("[\\-\\d\\s]+");
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文