Search code examples
pythonregexregex-lookarounds

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 ::.


Solution

  • 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.