Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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 :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading