I have a regex that looks like this:
/^(.*?)( tom.*)?$/
I execute it on the string
call tomorrow
My matching groups for this is going to be
1. `call`
2. ` tomorrow`
However, notice that because the second matching group is optional, the first wildcard could consume the whole string and the match would still be valid. This is exactly what happens if you make the first wildcard greedy by removing the question mark.
1. `call tomorrow`
So my question is: is there any way to instruct the regex engine that I want all valid matches to the string, not just the first one (based upon laziness/greediness)? I acknowledge that this may be slow, but it's necessary for my case.
To clarify, I want to parse the string call tomorrow
and have it return:
MATCH 1
1. `call`
2. ` tomorrow`
MATCH 2
1. `call tomorrow`
When the Regex engine encounters the (.*?)
, it is going to consume 0 characters and then try the rest of the string. When that fails, it will try with 1 character, then 2, then 3, then 4. When it hits 4 characters, (call
) the regex will parse to the end, and quit. I want a way to say "parse again, but start with that wildcard consuming 5 characters, then 6, then 7..." Eventually, it will try consuming 13 characters (call tomorrow
), which will also allow the rest of the regex to parse to completion, and return that result.
Please note that this is not a question about the /g/
flag - the index of the match is not changing.
If this is not possible, is Regex the wrong tool for this application? What should I be using instead?