I am trying to write a regular expression in python to remove digits except those between brackets here is an example : "[Verse 1: Bankroll Fresh] Dope boy, '[9]5 Air Max on Came from 1952.."
I would like : "[Verse 1: Bankroll Fresh] Dope boy, '[9] Air Max on Came from .."
CodePudding user response:
Using re.sub(pattern, replacement, string)
output = re.sub(r"(?!\d \])\d ", "", input_string)
(?!...) is a negative lookahead (aka. do not find the pattern).
\d \] finds digit/s followed by a ].
\d is digit/s.
For more information:
CodePudding user response:
Can you try this regex r'(?<!\[)\d (?!\])'?
st = "[Verse 1: Bankroll Fresh] Dope boy, '[9]5 Air Max on Came from 1952.."
st = re.sub(r'\[\D*\d\D*\]|(?<!\[)\d (?!\])', lambda x: x.group(0) if x.group(0).startswith('[') else '', st)
# [Verse 1: Bankroll Fresh] Dope boy, '[9] Air Max on Came from ..
print(st)
(?<!\[) and (?!\]) are matches if \d NOT preceded by [ and NOT followed by ], respectively.
