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 regex keep text between the last two occurrences of a character

As the title says, I want to extract the text between the last two ocurrences of a character in a string.

I have:

'9500 anti-Xa IU/ml - 0,6 ml 5700 IU -'
'120 mg/ml – 0.165 ml -'
'300-300-300 IR/ml  or  IC/ml - 10 ml -'
'Fluocortolone-21-pivalate 1 mg/g, Lidocaine hydrochloride 20 mg/g - 15 g -'

I want to have:

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

'0,6 ml 5700 IU'
'0.165 ml'
'10 ml'
'15 g'

I tried using -\s*.*- but it matches everything between first and last -. What’s the correct regex to use?

>Solution :

With search:

import re
[re.search(r'[-–]\s*([^-–]+?)\s*[-–][^-–]*$', x).group(1) for x in l]

Or split:

[re.split(r'\s+[-–]\s*', x, 2)[-2] for x in l]

output: ['0,6 ml 5700 IU', '0.165 ml', '10 ml', '15 g']

used input:

l = ['9500 anti-Xa IU/ml - 0,6 ml 5700 IU -',
     '120 mg/ml – 0.165 ml -',
     '300-300-300 IR/ml  or  IC/ml - 10 ml -',
     'Fluocortolone-21-pivalate 1 mg/g, Lidocaine hydrochloride 20 mg/g - 15 g -'
    ]

regex demo

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