In the following string blah blahTESTblab la14blah-15S rebla
I'm trying to capture TEST
and 15S
if present.
I wrote the following regex : (TEST).*?(\d{1,2}S)?
Everything is going well until the last ?
. The last group really needs to be optional.
And groups can't be anchored, as strings I'm searching in are human written, ie tot reliable.
What's wrong ?
The following pattern seems to do what you want:
(TEST)(?:.*?(\d{1,2}S)|)
This says to match:
(TEST)
a required "TEST" in the first capture group(?:
do not capture
.*?(\d{1,2}S)
match either any string ending in e.g. "15S" in the second capture group|
OR)
end non capture group