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:

'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

Leave a Reply