Search code examples
pythonregexcharacter-class

REGEX: \Z in character class


REGEX:

((?<=blah)[^@\Z]+)

It supposed to capture symbols symbols that preceded by blah and ending with @ or end of string. Is correct technique to enclose \Z in character class ?


Solution

  • No, character classes can only contain literal characters or other character classes; your example matches anything that is not a @, the \Z anchor, is ignored as it is not a character class itself. Note that the ^ caret negates the character group. Use a group instead with a | 'or' symbol instead:

    ((?<=blah)(?:[^@]+|\Z))
    

    I used a non-capturing group there ((?:...)) to group the two options. The group matches any characters that are not @, or it matches the \Z end-of-string anchor.