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
  • 12,665
  • 15
  • 42
  • 74
crazy_dd
  • 115
  • 1
  • 11
  • 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
  • 52,115
  • 13
  • 74
  • 94