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 ?
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 ?
Share Improve this question asked Mar 18 at 14:02 user9428517user9428517 234 bronze badges2 Answers
Reset to default 1The 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- (match nothing at all beyond the first "TEST")
)
end non capture group
Demo
Thanks again for this answer. I modified your regex a bit in order to work correctly with the libreoffice regex function. I added .*
twice, at the beginning and at the end of the regex.
With blah blahTESTblab la14blah-15S rebla
in cell A1
:
=REGEX(A1;"(?i)(TEST)(?:.*(\d{1,2}S)|)";"★$1$2")
givesblah blah★TEST15S rebla
=REGEX(A1;"(?i).*(TEST)(?:.*(\d{1,2}S)|).*";"★$1$2")
gives★TEST15S