Need help with a regular expression

S

Sharun

Python newbie here. I am not clear about how the matching is taking
place when I do the following
str5 = 'aaa bbb\r\n ccc ddd\r\n eee fff'
re5=re.compile('aaa.*(ddd|fff)',re.S);
re5.search(str5).group(0)
'aaa bbb\r\n ccc ddd\r\n eee fff'
re5.search(str5).group(1)
'fff'

I am trying to find the substring starting with 'aaa', and ending with
ddd OR fff. If ddd is found shouldnt the search stop? Shouldn't
re5.search(str5).group(0) return 'aaa bbb\r\n ccc ddd' ?

Thanks
 
M

marek.rocki

I am trying to find the substring starting with 'aaa', and ending with
ddd OR fff. If ddd is found shouldnt the search stop? Shouldn't
re5.search(str5).group(0) return 'aaa bbb\r\n ccc ddd' ?

The documentation for the re module (http://docs.python.org/lib/re-
syntax.html), tells you that the "*", "+", and "?" qualifiers are all
greedy; they match as much text as possible. What you are looking for
are the qualifiers "*?", "+?", "??". Your regex pattern might look
like this: "aaa.*?(ddd|fff)".

Regards,
Marek
 
P

Paddy

Python newbie here. I am not clear about how the matching is taking
place when I do the following


'aaa bbb\r\n ccc ddd\r\n eee fff'


'fff'

I am trying to find the substring starting with 'aaa', and ending with
ddd OR fff. If ddd is found shouldnt the search stop? Shouldn't
re5.search(str5).group(0) return 'aaa bbb\r\n ccc ddd' ?

Thanks

Have an RE problem in Python?

Get Kodos! (http://kodos.sourceforge.net/)

- Paddy.
 

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

Forum statistics

Threads
473,994
Messages
2,570,223
Members
46,810
Latest member
Kassie0918

Latest Threads

Top