Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Match only if following string matches pattern

I'm trying to match an entire string that starts with a certain string and then match any number of characters except ::, if :: was matched then only accept if followed by the string CASE.

So for example: A string that starts with Linus:: followed by 0 or more 1 characters except if :: then CASE has to follow else only matches everything before the ::.

Linus::AOPKNS::CASE would capture the entire string

Linus::AOPKNS would capture the entire string

Linus::AOPKNS::OK would only capture Linus::AOPKNS

I imagine I'd have to use a positive lookahead but I'm not quite sure how to do that considering I wanna match any number of characters before the ::.

like image 782
NOT Avatar asked Oct 27 '25 09:10

NOT


1 Answers

Use a tempered greedy token:

^                  # Match at the start of the string
Linus::            # 'Linus::', literally,
(?:(?!::).)+       # followed by a sequence of characters that doesn't contain '::'
(?:::CASE)?        # and, optionally, '::CASE'.

Try it on regex101.com.

Depending on your use case, you might want to add a \b (word boundary) at the end of the pattern.

like image 166
InSync Avatar answered Oct 28 '25 23:10

InSync