Using Regex to remove brackets and contents if the contents contain any non digits

Viewed 119

I want to remove the [] and contents if there is at least one non digit within the [].

Input: Tag1[aA], Tag2[55].AA[*], Tag3[A1];

Output: Tag1, Tag2[55].AA, Tag3;

I have tried the below, however it only works on exact matches within the brackets.

import re

line = "'Tag1[aA], Tag2[55].AA[*], Tag3[A1];"

# removes the [] if contents contain non digits only
pattern = r'\[\D+\]'
s = re.sub(pattern, '', line)
print(s)

> "'Tag1, Tag2[55].AA, Tag3[A1];"

3 Answers
import re

line = "'Tag1[aA], Tag2[55].AA[*], Tag3[A1];"
re.sub(r'\[[a-zA-Z*]{1,}[\d]{0,}\]', r'', line)

Use this code, I have considered some additional cases also.

import re

line = "'Tag1[aA], Tag2[55].AA[*], Tag3[A1],Tag4[34],Tag5[1B];"

# removes the [] if contents contain non digits only
pattern = r'(\[\D.+?\]?|\[.\D.+?\]?)'
s = re.sub(pattern, '', line)
print(s)

Output: 'Tag1, Tag2[55].AA, Tag3,Tag4[34],Tag5;

You can use:

\[[^][]*[^][\d][^][]*]

The pattern matches:

  • \[ Match [
  • [^][\d]* Optionally match any char except [ and ]
  • [^][\d] Match a single char other than [ ] or a digit
  • [^][\d]* Optionally match any char except [ and ]
  • ] Match ]

Regex demo

import re

print(re.sub(r"\[[^][]*[^][\d][^][]*]", "", "Tag1[aA], Tag2[55].AA[*], Tag3[A1];"))

Output

Tag1, Tag2[55].AA, Tag3;
Related