How to match the beginning of a line or character in Python

I have a line 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 the selected parameter, i.e.

  • par='be' → need 0.209
  • par='e' → need 0.999

I tried:

 num1 = float(re.findall(par + '(\d+\.\d*)', dir)[0]) 

but for par='e' it will correspond to 0.209 and 0.999 , so I tried to match the parameter along with the beginning of the line or underscore:

 num1 = float(re.findall('[^_]'+par+'(\d+\.\d*)', dir)[0]) 

which for some reason did not work.

Any suggestions? Thanks!

+4
source share
4 answers

Your pattern [^_] matches any character that is not an underscore.

Use (..|..) or a grouping instead:

 float(re.findall('(?:^|_)' + par + r'(\d+\.\d*)', dir)[0]) 

I used there a group (?:..) non-captureuring so that it does not 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 develop using a group of characters ( [..] ), and you start this group with a carriage ( ^ ), you invert the group of characters by flipping it from matching the specified characters to match all the others:

 >>> re.findall('[a]', 'abcd') ['a'] >>> re.findall('[^a]', 'abcd') ['b', 'c', 'd'] 
+2
source

without regex solution:

 def func(par,strs): ind=strs.index('_'+par)+1+len(par) ind1=strs.find('_',ind) if strs.find('_',ind)!=-1 else len(strs) return strs[ind:ind1] 

output:

 >>> func('be',dir) '0.209' >>> func('e',dir) '0.999' >>> func('cc',dir) '1.3000' 
0
source

Solution without regex:

 >>> def get_value(dir, parm): ... return map(float, [t[len(parm):] for t in dir.split('_') if t.startswith(parm)]) ... >>> get_value('a1.8000_b1.0000_cc1.3000_al0.209_be0.209_c1.344_e0.999', "be") [0.20899999999999999] 

If there are several occurrences of a parameter in a string, they are all evaluated.

And the non-cast version in float:

 return [t[len(parm):] for t in dir.split('_') if t.startswith(parm)] 
0
source
 (?P<param>[a-zA-Z]*)(?P<version>[^_]*) 
0
source

All Articles