I have a string
s = 'ABC123ABC 23AB'
I need to replace the 23AB
with '', so the result is:
s = 'ABC123ABC'
I have tried:
s = re.sub('\d+AB', '', s)
but this also replaces it in ABC123ABC
I have a string
s = 'ABC123ABC 23AB'
I need to replace the 23AB
with '', so the result is:
s = 'ABC123ABC'
I have tried:
s = re.sub('\d+AB', '', s)
but this also replaces it in ABC123ABC
You could add \b
which is word boundary to ensure there is no other stuff around what you look for, and \s*
to remove the spaces too
import re
val = "ABC123ABC 23AB"
val = re.sub(r'\s*\b\d+AB\b', '', val)
print(val)