Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

More than 1 matches for regular expression

Status
Not open for further replies.

Patrick2000

Programmer
Dec 5, 2001
1
US
Hi,

I am using regular expression to search a string
like 'CEO.*ages', I intends to only get the CEO's name
between CEO and ages, however the python returned the
longest match like 'CEO Name ages, VP Name ages, etc'
does anybody know how to accomplish this?

Thanks
Pat
 
Patrick

I have not tried this specifically in Python, but it works for general RegEx searches:

like '^CEO.ages$'

The character '^' means the following string should be returned only if it comes at the start of the line.

The character '$' means the preceding string should be returned only if it comes at the end of the line.
 
'CEO.*ages' is correct, but .* means "eat any character that comes next". This includes the 'ages', and the rest of the file. * is called greddy.

You must use a non-greedy pattern:
'CEO.*?ages'

In this case .*? means: "eat any character that comes next, except if next word is 'ages'.

If you want to experiment with regular expressions, you can download the opensource JEdit editor (great program) and the ReTest module for JEdit:
You will be able to experiment with regular expressions and see the result in realtime.

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top