1

I want to remove numbers in my string but keep alphanumeric as is using regex in python.

" How to remove 123 but keep abc123 from this question?"

I want result to be like:

"How to remove but keep abc123 from this question?"

I tried

spen=re.sub('[0-9]+', '', que)

but it removes all numbers. I want abc123 to be as is.

Tonechas
  • 13,398
  • 16
  • 46
  • 80
crazy_dd
  • 125
  • 1
  • 12
  • How is that a duplicate of *Python regular expression match whole word* ? This is the same type of answer, but the questions have nothing to do with each other... – Gawil May 31 '17 at 13:12

1 Answers1

8

You could use the word boundary symbol \b, something like this:

re.sub(r'\b[0-9]+\b', '', que)

That will not match numbers that are part of a longer word.

khelwood
  • 55,782
  • 14
  • 81
  • 108