2

How to replace only integers in a string by a string

My script:

x="the number 1234, today the 909090 of the value of the 90.94"
# by the way the has to be proposed to get the123abd 123the" by a string "ItWasanINT""

re.sub(r'\b\d+(?!\.\d+)\b','ItWasanINT',x)

...Not working

2
  • 4
    You mean the 90.94 value should not be replaced? Commented Sep 18, 2012 at 18:59
  • Why don't you document the post-substitution value your code generates, and what you'd like it to generate? Commented Sep 19, 2012 at 0:00

1 Answer 1

4

You probably want to exclude numbers that are both not preceded by a "number." combo and not followed by a ".number" combo:

re.sub(r'\b(?<!\d\.)\d+(?!.\d)\b','ItWasanINT', x)

Result:

>>> import re
>>> x="the number 1234, today the 909090 of the value of the 90.94"
>>> re.sub(r'\b(?<!\d\.)\d+(?!\.\d)\b','ItWasanINT',x)
'the number ItWasanINT, today the ItWasanINT of the value of the 90.94'
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.