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

C

Captain Dunsel

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

MRAB

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!
How about searching with a pattern like r':(\d*)FUDD, ELMER'.
 
J

Jussi Piitulainen

Captain said:
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.
'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>
 
J

Justin Barber

I'm guessing that the name "FUDD, ELMER" varies. In that case, you might try something like this:
['624309']

This would account for first names such as 'Mary Ann' and also automatically matches characters only to the end of the line, since you have not flagged re.DOTALL.

Justin
 
C

Captain Dunsel

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!

Thanks for your help, everyone!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top