I am trying to parse an input string using a regular expression. I am getting a problem when trying to capture a repeating group. I always seem to be matching last instance of the group. I have tried using Reluctant (non greedy) quantifiers, but I seems to be missing something. Can someone help?
Regular expression tried:
(OS)\\s((\\w{3})(([A-Za-z0-9]{2})|(\\w{3})(\\w{3}))\\/{0,1}){1,5}?\\r
(OS)\\s((\\w{3}?)(([A-Za-z0-9]{2}?)|(\\w{3}?)(\\w{3}?))\\/{0,1}?){1,5}?\\r
Input String:
OS BENKL/LHRBA/MANQFL\r\n
I always seem to get last group which is MANQFL group (MAN QFL)
, and my aim is to get all three groups (there can be 1-5 groups):
(BEN KL) , (LHR BA) and (MAN QFL).
C++ code snippet:
std::string::const_iterator start = str.begin(), end = str.end();
while(regex_search(start,end,what,expr))
{
cout << what[0];
cout << what[1];
...
start += what.position () + what.length ();
}
This loop only exceutes once, while I expect it to run 3 times in this example. Any help will be much appreciated.