0

こんにちは、次の式の値を取得したいと考えています: POLYGON(100 20, 30 40, 20 10, 21 21) POLYGON(100 20, 30 40, 20 10, 21 21) を検索しています

次のコードを実行すると、次の結果が得られます:
POLYGON(100 20, 30 40, 20 10, 21 21)
結果 = 100 20
r2 = 100
r2 = 20
r2 = , 21 21
r2 = 21
サイズ = 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;
}
4

2 に答える 2

2

私は正規表現の専門家ではありませんが、あなたの正規表現を読んだところ、正しいようです。

このフォーラムの投稿は、Boost.Regex が正規表現の最後の結果のみを返すという、まったく同じことについて話しているようです。どうやらデフォルトでは、Boost はマッチの繰り返しの最後のマッチのみを追跡します。ただし、これを変更できる実験的な機能があります。詳細については、「繰り返しキャプチャ」の下をご覧ください。

ただし、他に2つの「解決策」があります。

  1. 正規表現を使用して最初の数字のペアを追跡し、そのペアを削除して部分文字列を取得し、すべての入力が得られるまで、その部分文字列に対して別の正規表現を実行します。

  2. Boost.Spiritを使用してください。おそらく、Boost.Regex よりも入力の解析に適しています。

于 2011-03-06T23:19:23.577 に答える
0

IRC チャンネルから結果を取得しました。正規表現は次のとおりです。

static const boost::regex ex("[\\d\\s]+");
static const boost::regex ex("[\\-\\d\\s]+");
于 2011-03-06T23:20:14.990 に答える