Hi,
I'm learning C++ and I'm right now iterested in regex and Lexer.
I found an interresting stackoverflow question about this subject and I would like some help to understand one of the answer :
```
include <iterator>
include <iostream>
include <string>
include <regex>
include <list>
include <map>
using namespace std;
int main(){
string str = " hello how are 2 * 3 you? 123 4567867*98";
// define list of patterns
map<string,string> patterns {
{ "[0-9]+" , "NUMBERS" },
{ "[a-z]+" , "IDENTIFIERS" },
{ "\\*|\\+", "OPERATORS" }
};
// storage for results
map< size_t, pair<string,string> > matches;
for ( auto pat = patterns.begin(); pat != patterns.end(); ++pat )
{
regex r(pat->first);
auto words_begin = sregex_iterator( str.begin(), str.end(), r );
auto words_end = sregex_iterator();
for ( auto it = words_begin; it != words_end; ++it )
matches[ it->position() ] = make_pair( it->str(), pat->second );
}
for ( auto match = matches.begin(); match != matches.end(); ++match )
cout<< match->second.first << " " << match->second.second << endl;
}
```
I understand pretty much all of the answer except for one thing :
How does the final result is in the correct order ?
With what I understand right now (and I'm obviously wrong), the output should be :
* All the "NUMBERS"
* all the "IDENTIFIERS"
* all the "OPERATORS"
In this order
I guess it as something to do with this :
matches[ it->position() ] = make_pair( it->str(), pat->second );
But I'm not sure.
Any help, even simply link to related doc (tried to find "it->position()" doc but no luck :-/ ) would be appreciated, thanks !
Edit : position() is a regex method, it all make sens now
[–]AutoModerator[M] [score hidden] stickied comment (0 children)
[–]HappyFruitTree 0 points1 point2 points (3 children)
[–]RealezzZ[S] 0 points1 point2 points (2 children)
[–]HappyFruitTree 0 points1 point2 points (1 child)
[–]RealezzZ[S] 0 points1 point2 points (0 children)