Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to match beginning of string or character in Python

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.209
  • par='e' -->need 0.999

I 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!

like image 369
user1638145 Avatar asked Aug 31 '12 08:08

user1638145


People also ask

How do you match the beginning of a string in Python?

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.

How do you match part of a string in Python?

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.


1 Answers

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']
like image 87
Martijn Pieters Avatar answered Oct 02 '22 09:10

Martijn Pieters