English 中文(简体)
不退还任何结果
原标题:Regex not returning any results
  • 时间:2012-01-13 17:04:22
  •  标签:
  • regex
  • boost

I have a few questions about boost::regex: I tried an example below.

(1) 哪怕是第4段的 s子? 它像“失败”一样健全,但为什么你们想要的是,而不是仅仅返回什么? 我在没有第4段的情况下对它进行了审判,但却没有汇编。

2) I am getting the output: (1, 0) (0, 0) (3, 0) (0, 0) (5, 0)

谁能解释什么是错的?

#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;
}
最佳回答

你的条例完全是任择的:

"[0-9]*\.?[0-9]*"

也有空洞。 http://www.un.org/Depts/DGACM/index_chinese.htm

您至少应规定:

"(?:[0-9]+(?:\.[0-9]*)?|\.[0-9]+)"

可查阅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
问题回答

暂无回答




相关问题
Uncommon regular expressions [closed]

Recently I discovered two amazing regular expression features: ?: and ?!. I was curious of other neat regex features. So maybe you would like to share some tricky regular expressions.

regex to trap img tag, both versions

I need to remove image tags from text, so both versions of the tag: <img src="" ... ></img> <img src="" ... />

C++, Boost regex, replace value function of matched value?

Specifically, I have an array of strings called val, and want to replace all instances of "%{n}%" in the input with val[n]. More generally, I want the replace value to be a function of the match ...

PowerShell -match operator and multiple groups

I have the following log entry that I am processing in PowerShell I m trying to extract all the activity names and durations using the -match operator but I am only getting one match group back. I m ...

Is it possible to negate a regular expression search?

I m building a lexical analysis engine in c#. For the most part it is done and works quite well. One of the features of my lexer is that it allows any user to input their own regular expressions. This ...

regex for four-digit numbers (or "default")

I need a regex for four-digit numbers separated by comma ("default" can also be a value). Examples: 6755 3452,8767,9865,8766,3454 7678,9876 1234,9867,6876,9865 default Note: "default" ...

热门标签