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

how to remove all parenthese , brackets , dashes and white space using one regex command?

Here is the string and I have managed to get rid of [] and ().

text='Lee  Jong-jae(Season1) Nam  Sung-woo[1]'

This one gets rid of both Parentheses and brackets?

 text.str.replace(r"[(\[].*?[\)\]]", "")

I tried to add regex for removing brackets and white spaces but to no avail.

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

text.str.replace(r"[(\[].*?[\)\]\\s-]+", "") 

I want it to look like this:

Lee Jong jae, Nam Sung woo

Thanks again for your help.

>Solution :

You can use

import re
text='Lee  Jong-jae(Season1) Nam  Sung-woo[1]'
pattern = r'(\[[^][]*]|\([^()]*\))|[\s-]+'
print(  re.sub(pattern, lambda m: ',' if m.group(1) else ' ', text).strip(', ') )

See the Python demo.

Output:

Lee Jong jae, Nam Sung woo

See the regex demo.

Details:

  • (\[[^][]*]|\([^()]*\)) – Group 1: substrings between the closest square or round brackets
  • | – or
  • [\s-]+ – one or more whitespaces or hyphens.

If Group 1 matches, the replacement is a comma, else, it is a space. Extra commas or spaces are stripped with strip(', ').

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