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

Time:06-21

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.

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

I want it to look like this:

Lee Jong jae, Nam Sung woo

Thanks again for your help.

CodePudding user response:

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(', ').

  • Related