1

I want to match every single number in the following string:

-0.237522264173E+01  0.110011117918E+01  0.563118085683E-01  0.540571836345E-01 -0.237680494785E+01  0.109394729137E+01 -0.237680494785E+01  0.109394729137E+01  0.392277532367E+02  0.478587433035E+02

However, for some reason the following boost::regex doesn't work:

(.*)(-?\\d+\\.\\d+E\\+\\d+ *){10}(.*)

What's wrong with it?

EDIT: posting relevant code:

std::ifstream plik("chains/peak-summary.txt");
std::string mystr((std::istreambuf_iterator<char>(plik)), std::istreambuf_iterator<char>());
plik.close();
boost::cmatch what;
boost::regex expression("(.*)(-?\\d+\\.\\d+E\\+\\d+ *){10}(.*)");
std::cout << "String to match against: \"" << mystr << "\"" << std::endl;
if(regex_match(mystr.c_str(), what, expression)) 
{ 
  std::cout << "Match!";
  std::cout << std::endl << what[0] << std::endl << what[1] << std::endl;
} else {
  std::cout << "No match." << std::endl;
}

output:

String to match against: " -0.237555275450E+01  0.109397523269E+01  0.560420828508E-01  0.556732715285E-01 -0.237472295761E+01  0.110192835331E+01 -0.237472295761E+01  0.110192835331E+01  0.393040553508E+02  0.478540190640E+02
"
No match.

Also posting the contents of file read into the string:

[dare2be@schroedinger multinest-peak]$ cat chains/peak-summary.txt 
 -0.237555275450E+01  0.109397523269E+01  0.560420828508E-01  0.556732715285E-01 -0.237472295761E+01  0.110192835331E+01 -0.237472295761E+01  0.110192835331E+01  0.393040553508E+02  0.478540190640E+02

2 Answers2

1

The (.*) around your regex match and consume all text at the start and end of the string, so if there are more than ten numbers, the first ones won't be matched.

Also, you're not allowing for negative exponents.

(-?\\d\\.\\d+E[+-]\\d+ *){10,}

should work.

This will match all of the numbers in a single string; if you want to match each number separately, you have to use (-?\\d\\.\\d+E[+-]\\d+) iteratively.

Tim Pietzcker
  • 328,213
  • 58
  • 503
  • 561
  • No luck - regex_match() returns no match for both cases. –  Sep 12 '11 at 11:44
  • Read the [docs](http://www.boost.org/doc/libs/1_39_0/libs/regex/doc/html/boost_regex/ref/regex_match.html): `regex_match()` needs to match the entire string; you want `regex_search()`. – Tim Pietzcker Sep 12 '11 at 12:21
  • Thank you - you pointed into the right direction; will edit your answer as soon as I apply all changes to my code. –  Sep 12 '11 at 12:42
0

Try with:

(-?[0-9]+\\.[0-9]+E[+-][0-9]+)

Your (.*) in the beggining matches greedy whole string.

hsz
  • 148,279
  • 62
  • 259
  • 315