dimanche 22 février 2015

How to use std::regex to find the next match in a string?

Trying to use std::regex in a scanner. So all it is supposed to do in my case is to find the first match starting at const char *p of the input sequence. It is not supposed to skip anything. It just needs to match as long as the expression is valid. Then return what it got.


Is that possible?


Here my humble attempt:



#include <regex>

static void Test()
{
const char *numbers = "500 42 399 4711";
std::regex expr("[0-9]+");
std::match_results<const char *> matches;
if (std::regex_match
(&numbers[0]
,&numbers[strlen(numbers)]
, matches
, expr
, std::regex_constants::match_continuous))
{
printf("match: %s\n", matches[0]);
}
else
puts("No match.");
}


What I am looking for is that it only returns "500" as a successful match. But I cannot even get it to return true...


Thanks.


Aucun commentaire:

Enregistrer un commentaire