Search code examples
pythonregexarabicpython-re

Sub all the specified regex interval except some characters


For example, I want to replace all the data going from the specified intervals with * (except the chars u0650, u0660, u064F), for example.

Note: I don't want to break the interval because I have a lot of characters to preserve.

data = re.sub(r'[\u0600-\u061E\u0620-\u065F\u0670-\u06ef]', "*", data)

Solution

  • You can put the characters to be excluded in a negative Lookahead before the main character class.

    For example:

    (?![\u0650\u0660\u064F])[\u0600-\u061E\u0620-\u065F\u0670-\u06ef]
    

    Demo.