-1

I have a sentence that consists of numbers and words. I am looking for a solution to remove numbers except at the beginning. I have tried the following code, but it's removing all the numbers.

x = '45 is fourth five 45 when 9 and 5 are multiplied'

re.sub('\d', '', x)

Its output was

'is Fourth five when and are multiplied'

But need the output in the following manner.

'45 is Fourth five when and are multiplied'

Note:

  1. The number may or may not present at the beginning.

  2. Inside the sentence, there can be alphanumeric (460KG), then it should be changed (KG). That means, except at the beginning, the number should be removed elsewhere. example :

6-apples price is 56Rs

I need an output as

6-apples price is Rs
Raghu
  • 181
  • 1
  • 8
  • 1
    Expected vs. desired. You have not shown any attempt to get the desired output, so saying it's expected is effectively looking for free code. – Mad Physicist Apr 07 '20 at 17:07
  • 1
    @Raghu this SO question and its answers should be sufficient information for you to solve this problem: https://stackoverflow.com/questions/1240504/regular-expression-to-match-string-starting-with-stop – Chris Apr 07 '20 at 17:13
  • 1
    Also, your problem is not clearly defined. For example, does the number always appear in the first character, or can it appear after arbitrary non-numeric sequences? – Mad Physicist Apr 07 '20 at 17:13
  • What exactly is your question? – AMC Apr 07 '20 at 19:45
  • @AMC and MadPhysicist I have improved the explanation, hope its sufficient. – Raghu Apr 08 '20 at 13:43

2 Answers2

1

You can use this:

x = '45 is fourth five 45 when 9 and 5 are multiplied'

string = re.sub(r'(?<!^)\b\d+\s', '', x)

Result:

>>> print(string)

45 is fourth five when and are multiplied
Shubham Sharma
  • 68,127
  • 6
  • 24
  • 53
0

Using Pypi regex library, you can do:

import regex

x = '45 is fourth five 45 when 9 and 5 are multiplied'
print regex.sub(r'(?<=\D*\d+\D+)\d+ ?', '', x)

Output:

45 is fourth five when and are multiplied

Explanation:

(?<=        # positive lookbehind, make sure we have before:
    \D*         # 0 or more non digits
    \d+         # 1 or more digits
    \D+         # 1 or more non digits
)           # end lookbehind
\d+         # 1 or more digits
 ?          # optional space
Toto
  • 89,455
  • 62
  • 89
  • 125