Search code examples
regexboost-regex

Constructing boost regex


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

Solution

  • 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.