带有 BoostRegex C++ 的正则表达式
您好,我希望获得以下表达式的值: 多边形(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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我不是正则表达式专家,但我读了你的正则表达式,它似乎是正确的。
此论坛帖子似乎在谈论完全相同的事情,其中Boost.Regex 仅返回正则表达式的最后一个结果。显然,默认情况下,Boost 仅跟踪重复匹配的最后一场比赛。但是,有一个实验性功能可以让您更改此设置。 更多信息,位于“重复捕获”下。
不过,还有另外 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:
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.
Use Boost.Spirit, it's probably more suited for parsing input than Boost.Regex.
我已经从 IRC 频道得到了结果。
正则表达式是:
I have got the result from IRC channel.
The regular expression is :