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

What's the proper regex for removing "?()"

I am trying to remove ?,() and " from the below string my initial matches are working but for these 3 it not matching kindly suggest what is the issue here

data = "(The rain - :in  ,Spain?)"

regexpat =  re.sub(r"'|,|;|.|:|/?|-|(|)", "", name)

I need all the special chars removed or replaced with blank

I tried with

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

-|:|\)|\?|

but substitution is breaking with this output *

*"*(*T*h*e* *r*a*i*n* ** **i*n* * *,*S*p*a*i*n***"*
*

Expected Output is

The rain in Spain

trying here – https://regex101.com/r/Ozsnzv/1

>Solution :

You can use a character class to shorten the pattern, and escape the dot to match it literally.

Using /? in an alternation matches an optional /, but you can just add the /to the character class. If you also meant to match ? you can also add that one.

Then change possible double spaces gaps to single spaces.

Note to use data instead of name in the call to re.sub.

import re

data = "(The rain - :in  ,Spain?)"
regexpat = re.sub(r"[',;.:/?()-]+", "", data)

print(' '.join(regexpat.split()))

Output

The rain in Spain

See a Python demo.

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