Regex for a replacing all special characters that are wrapped by normal characters

The following code snippet doesn’t seem to work. I want "This$#is" to be changed to "This is" whereas "This$#" should remain as "This$#" since it does not have normal characters at the end.

import re

script = "This$#is"
sc = re.sub("^[A-Za-z]+([$#]+)[A-Za-z]+", " ", script)
print(sc)

>Solution :

You can match 1 or more repetitions of the character class and assert a char a-z to the right:

[$#]+(?=[A-Za-z])

See the match on regex101.

import re

script = "This$#is\nThis$#"
sc = re.sub(r"[$#]+(?=[A-Za-z])", " ", script)
print(sc)

Output

This is
This$#

If you want to keep the anchor for the start of the string, you can also use a capture group and use that in the replacement.

import re

script = "This$#is\nThis$#"
sc = re.sub("^([A-Za-z]+)[$#]+(?=[A-Za-z])", r"\1 ", script)
print(sc)

Leave a Reply