Reputation: 1780
I'm trying to draft a regex that will match everything before the first :
in a string, and exclude a specific term in the text right before the :
if found. Call the term Grumble
.
I want to match Foo
in the string Foo: Bars
.
I want to match Foo fuzz
in Foo fuzz Grumble: Bars and moar bars
.
I tried the pattern ^.*(?=(Grumble)?:)
, but it includes Grumble
in the match in the second example above.
Upvotes: 1
Views: 1591
Reputation: 4981
How about:
^.*?(?=Grumble|:)
.*?
non-greedy match for any character(?=Grumble|:)
denotes a positive lookahead with two conditions
Grumble
is found then only capture until before Grumble
:
is found then only capture until before :
EDIT: Mis-interpreted the OP's ask a tiny bit. Thanks @revo for the clarification
Upvotes: 1