1

I have a python string:

s = 'filename               13.00 50850.8732503344475    37.11  abc'

In order to find the second string with format nn.nn, I know I can do:

re.findall(r'.*(\b\d+\.\d+)',s)[0]

which finds:

'37.11'

But I want to replace it with 99.99.

I tried:

re.sub(r'.*(\b\d+\.\d+)','99.99',s)

But that just yields:

'99.99  abc'

whereas I want:

'filename               13.00 50850.8732503344475    99.99  abc'

Clearly I don't yet understand how regex works. Could someone offer help please?

0

2 Answers 2

2

You should capture what you need to keep and use the unambiguous replaement backreference in the replacement pattern:

s = re.sub(r'(.*)\b\d+\.\d+',r'\g<1>99.99', s)

See the Python demo and the regex demo.

Pattern details

  • (.*) - Group 1 (its value is referred to with \g<1> backreference from the replacement pattern): any 0+ chars other than line break chars as many as possible
  • \b - a word boundary
  • \d+ - 1+ digits
  • \. - a dot
  • \d+ - 1+ digits.
Sign up to request clarification or add additional context in comments.

Comments

1

Alternatively, you can replace last occurrence of simple pattern (\d+\.\d+):

s = "filename               13.00 50850.8732503344475    37.11  abc"
*_, last = re.finditer(r"(\d+\.\d+)", s)
s = s[:last.start()] + "99.99" + s[last.end():]

It's a bit faster. Results of timeit benchmark(code):

re.finditer() -> 11.30306268
re.sub() -> 15.613837582000002

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.