Search code examples
regexpython-re

Regex - everything after the last occurrence


I have this text:
ABC anything about the sky ABC and anything about the moon

I'm trying to write a REGEX to get everything after the last occurrence of 'ABC':

Example:

ABC anything about the sky ABC and anything about the moon
and anything about the moon

ABC anything about the sky ABC and anything ABC about the moon
about the moon

I was trying the expression below, but it considers ABC as separatelly letters, so it would match wrong if there was a single 'A' after 'ABC' in the text:

[^ABC]*$

PS: I'm trying an expression which does not make groups and i'm using python3


Solution

  • Without using a group, you could make use of lookarounds:

    (?<=\bABC\s)(?!.*\bABC\b).*
    
    • (?<=\bABC\s) Positive lookbehind, assert ABC and a whitespace char to the left
    • (?!.*\bABC\b) Negative lookahead, assert no occurrence of ABC to the right
    • .* Match any char 0+ times (or .+ if there should at least be a single character)

    Regex demo | Python demo


    If you want to support quantifier in the lookbehind assertion, you could use the PyPi regex module. For example

    (?<=\bABC \d+ )(?!.*\bABC\b).*
    

    Regex demo | Pyhon demo