开发者

Regex with BoostRegex C++

开发者 https://www.devze.com 2023-02-15 08:16 出处:网络
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)

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] &l开发者_如何学Ct;< 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;
}


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.


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]+");
0

精彩评论

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