dimanche 15 septembre 2019

C++ regex get matches and groups [duplicate]

This question already has an answer here:

I have the following regex: ([0-9]+)\/([0-9]*)\/([0-9]*) (properly escaped in my code). As you can see, this has three capture groups: one that must contain at least one number, and two additional groups (that may be empty).

I'm trying to run this over a string that should produce three matches for the entire regex. For example:

f 5/1/1 1/2/1 4/3/1

In this case, the result of the regex should be the following:

Match 1: 5/1/1, Group 1: 5, Group 2: 1, Group 3: 1

Match 2: 1/2/1, Group 1: 1, Group 2: 2, Group 3: 1

Match 3: 4/3/1, Group 1: 4, Group 2: 3, Group 3: 1

However, the way I understand it, C++11 can't return both the matches and the groups.

If I were to run the following code,

std::smatch matchs;
std::regex_search("f 5/1/1 1/2/1 4/3/1", matches "([0-9]+)\\/([0-9]*)\\/([0-9]*)");

matches would have 10 elements: matches[0] would be everything from 5 to the end, and matches[1]-matches[9] would have the capture groups.

Is this correct? And/or is there some way to easily get both matches and capture groups?

Aucun commentaire:

Enregistrer un commentaire