I have a string consisting of parameter number _ parameter number:
dir = 'a1.8000_b1.0000_cc1.3000_al0.209_be0.209_c1.344_e0.999'
I need to get the number behind a parameter chosen, i.e.
par='be'
-->need 0.209par='e'
-->need 0.999I tried:
num1 = float(re.findall(par + '(\d+\.\d*)', dir)[0])
but for par='e'
this will match 0.209
and 0.999
, so I tried to match the parameter together with the beginning of the string or an underscore:
num1 = float(re.findall('[^_]'+par+'(\d+\.\d*)', dir)[0])
which didn't work for some reason.
Any suggestions? Thank you!
Python Re Start-of-String (^) Regex. You can use the caret operator ^ to match the beginning of the string. For example, this is useful if you want to ensure that a pattern appears at the beginning of a string.
Use the in operator for partial matches, i.e., whether one string contains the other string. x in y returns True if x is contained in y ( x is a substring of y ), and False if it is not. If each character of x is contained in y discretely, False is returned.
Your [^_]
pattern matches any character that is not the underscore.
Use a (..|..)
or grouping instead:
float(re.findall('(?:^|_)' + par + r'(\d+\.\d*)', dir)[0])
I used a (?:..)
non-capturing group there so that it doesn't interfere with your original group indices.
Demo:
>>> import re
>>> dir = 'a1.8000_b1.0000_cc1.3000_al0.209_be0.209_c1.344_e0.999'
>>> par = 'e'
>>> re.findall('(?:^|_)' + par + r'(\d+\.\d*)', dir)
['0.999']
>>> par = 'a'
>>> re.findall('(?:^|_)' + par + r'(\d+\.\d*)', dir)
['1.8000']
To elaborate, when using a character group ([..]
) and you start that group with the caret (^
) you invert the character group, turning it from matching the listed characters to matching everything else instead:
>>> re.findall('[a]', 'abcd')
['a']
>>> re.findall('[^a]', 'abcd')
['b', 'c', 'd']
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With