Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Python replace between two chars (no split function)

I currently investigate a problem that I want to replace something in a string.

For example. I have the following string:

‘123.49, 19.30, 02\n’

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

I only want the first two numbers like ‘123.49, 19.30’. The split function is not possible, because a I have a lot of data and some with and some without the last number.

I tried something like this:

import re as regex

#result = regex.match(', (.*)\n', string)
result = re.search(', (.*)\\n', string)
print(result.group(1))

This is not working finde. Can someone help me?

Thanks in advance

>Solution :

You could do something like this:

reg=r'(\d+\.\d+), (\d+\.\d+).*'
if(re.search(reg, your_text)):
  match = re.search(reg, your_text)
  first_num = match.group(1)
  second_num = match.group(2)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading