Home > Back-end >  Converting python regular expression pattern to except
Converting python regular expression pattern to except

Time:09-23

I have regular expression patterns that I would like to use but instead of it looking for the characters in the pattern, I would like it to search for everything except these characters. I know that a "^" can be used to indicate except, however I don't know where or how to place it so that the patterns works. What changes can I make to the patterns so that they produce the desired outcome?

Current regex patterns:
([0-9] [,\-]*[0-9]*[ ]?|Self-employed)
and
\d[^\s]*

CodePudding user response:

Use re.sub() to replace the pattern with an empty string. The resulting string is everything except the parts that match the regexp.

rest = re.sub(r'\d[^\s]*', '', string)
  • Related