-2

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

Evan Brittain
  • 477
  • 3
  • 10

1 Answers1

-1

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)
azro
  • 47,041
  • 7
  • 30
  • 65