New to using re. Search for a number before a string.

Jussi Piitulainen jpiitula at ling.helsinki.fi
Sat Nov 2 08:06:20 CET 2013


Captain Dunsel writes:

> I have a text file that has lines with numbers occasionally
> appearing right before a person's name.  For example:
> 
> COLLEGE:ENROLLMENT:COMPLETED EVALUATIONS:624309FUDD, ELMER
> 
> where I want to search for the name "ELMER FUDD" and extract the
> number right in front of it "608309" when such a number appears but
> the length of the number is variable and using ?<= in a regular
> expression will only search for a fixed length.
> 
> Any ideas appreciated! 

Search for the digits and the name together. Make the digits a group
by putting their pattern in parentheses. If there is a match object,
extract the group.

 >>> line = 'COLLEGE:ENROLLMENT:COMPLETED EVALUATIONS:624309FUDD, ELMER'
 >>> m = re.search(r':(\d*)ELMER FUDD$', line)
 >>> m.group(1) if m else 'not found'
 'not found'
 >>> m = re.search(r':(\d*)FUDD, ELMER$', line)
 >>> m.group(1) if m else 'not found'
 '624309'

If you want a match only when there are digits, use \d+ instead.

Look at regex.search and match.group here:
<http://docs.python.org/3/library/re.html>



More information about the Python-list mailing list