Regex 正则表达式不返回任何结果
关于boost::regex,我有几个问题:我尝试了下面的一个例子 1) sregex_令牌迭代器的第四个参数是什么?这听起来像是一个“默认匹配”,但你为什么想要它而不是什么都不返回呢?我在没有第四个参数的情况下尝试了它,但它无法编译 2) 我得到了输出: (1, 0) (0, 0) (3, 0) (0, 0) (5,0) 有人能解释出哪里出了问题吗Regex 正则表达式不返回任何结果,regex,boost,Regex,Boost,关于boost::regex,我有几个问题:我尝试了下面的一个例子 1) sregex_令牌迭代器的第四个参数是什么?这听起来像是一个“默认匹配”,但你为什么想要它而不是什么都不返回呢?我在没有第四个参数的情况下尝试了它,但它无法编译 2) 我得到了输出: (1, 0) (0, 0) (3, 0) (0, 0) (5,0) 有人能解释出哪里出了问题吗 #include <iostream> #include <sstream> #include <vector>
#include <iostream>
#include <sstream>
#include <vector>
#include <boost/regex.hpp>
// This example extracts X and Y from ( X , Y ), (X,Y), (X, Y), etc.
struct Point
{
int X;
int Y;
Point(int x, int y): X(x), Y(y){}
};
typedef std::vector<Point> Polygon;
int main()
{
Polygon poly;
std::string s = "Polygon: (1.1,2.2), (3, 4), (5,6)";
std::string floatRegEx = "[0-9]*\\.?[0-9]*"; // zero or more numerical characters as you want, then an optional '.', then zero or more numerical characters.
// The \\. is for \. because the first \ is the c++ escpape character and the second \ is the regex escape character
//const boost::regex r("(\\d+),(\\d+)");
const boost::regex r("(\\s*" + floatRegEx + "\\s*,\\s*" + floatRegEx + "\\s*)");
// \s is white space. We want this to allow (2,3) as well as (2, 3) or ( 2 , 3 ) etc.
const boost::sregex_token_iterator end;
std::vector<int> v; // This type has nothing to do with the type of objects you will be extracting
v.push_back(1);
v.push_back(2);
for (boost::sregex_token_iterator i(s.begin(), s.end(), r, v); i != end;)
{
std::stringstream ssX;
ssX << (*i).str();
float x;
ssX >> x;
++i;
std::stringstream ssY;
ssY << (*i).str();
float y;
ssY >> y;
++i;
poly.push_back(Point(x, y));
}
for(size_t i = 0; i < poly.size(); ++i)
{
std::cout << "(" << poly[i].X << ", " << poly[i].Y << ")" << std::endl;
}
std::cout << std::endl;
return 0;
}
#包括
#包括
#包括
#包括
//本例从(X,Y)、(X,Y)、(X,Y)等中提取X和Y。
结构点
{
int X;
int-Y;
点(intx,inty):x(x),y(y){
};
向量多边形;
int main()
{
多边形多边形;
std::string s=“多边形:(1.1,2.2)、(3,4)、(5,6)”;
std::string floatRegEx=“[0-9]*\\.?[0-9]*”;//根据需要设置零个或多个数字字符,然后是可选的“.”,然后是零个或多个数字字符。
/\\\\\\\\因为第一个是C++ EclipPe字符,第二个是正则表达式转义字符。
//常量boost::正则表达式r(“(\\d+),(\\d+)”;
常量boost::regex r(“(\\s*”+floatRegEx+“\\s*,\\s*”+floatRegEx+“\\s*)”);
//\s是空白。我们希望允许(2,3)以及(2,3)或(2,3)等。
const boost::sregex_token_迭代器end;
std::vector v;//此类型与要提取的对象类型无关
v、 推回(1);
v、 推回(2);
对于(boost::sregex_token_迭代器i(s.begin(),s.end(),r,v);i!=end;)
{
std::stringstream-ssX;
ssX>x;
++一,;
std::stringstream-ssY;
ssY>y;
++一,;
多边形推回(点(x,y));
}
对于(大小i=0;i std::cout您的正则表达式是完全可选的:
"[0-9]*\\.?[0-9]*"
也匹配空字符串。因此“(\\s*”+floatRegEx+“\\s*,\\s*”+floatRegEx+“\\s*)”
也匹配一个逗号
你应该至少做一些强制性的事情:
"(?:[0-9]+(?:\\.[0-9]*)?|\\.[0-9]+)"
这允许1
,1.1
,1.
或.1
,但不允许
:
(?: # Either match...
[0-9]+ # one or more digits, then
(?: # try to match...
\. # a dot
[0-9]* # and optional digits
)? # optionally.
| # Or match...
\.[0-9]+ # a dot and one or more digits.
) # End of alternation
Tim,我以为?只是让点成为可选的?我确实想允许1。以及。1,这就是我使用[0-9]的原因*在小数点的两边。我如何使.optional成为可选的?另外,我用更合适的解析更新了问题。但是,我得到了5个输出,而不是我预期的3个输出?好的,我编辑了我的正则表达式。很快将添加一个解释。*
也使数字成为可选的。谢谢Tim。这肯定比使用全称要好但是,用硬编码输入,我的非鲁棒表达式仍然有效,我猜我现在做的是C++错误,因为我在原始文章中显示了2的输出。抱歉,我不知道如何使用Google库使用正则表达式。我从来没有写过C++代码。我也不会。我不想引入更多的依赖项。