Re: regular expression: perl == python

2004-12-23 Thread Nick Craig-Wood
Fredrik Lundh [EMAIL PROTECTED] wrote: that's not a very efficient way to match multiple patterns, though. a much better way is to combine the patterns into a single one, and use the lastindex attribute to figure out which one that matched. lastindex is useful, yes. see

Re: regular expression: perl == python

2004-12-23 Thread Fredrik Lundh
Nick Craig-Wood wrote: I take your point. However I don't find the below very readable - making 5 small regexps into 1 big one, plus a game of count the brackets doesn't strike me as a huge win... if you're doing that a lot, you might wish to create a helper function. the undocumented

Re: regular expression: perl == python

2004-12-23 Thread Nick Craig-Wood
Fredrik Lundh [EMAIL PROTECTED] wrote: the undocumented sre.Scanner provides a ready-made mechanism for this kind of RE matching; see http://aspn.activestate.com/ASPN/Mail/Message/python-dev/1614344 for some discussion. here's (a slight variation of) the code example they're

Re: regular expression: perl == python

2004-12-22 Thread Doug Holton
Fredrik Lundh wrote: JZ [EMAIL PROTECTED] wrote: import re line = The food is under the bar in the barn. if re.search(r'foo(.*)bar',line): print 'got %s\n' % _.group(1) Traceback (most recent call last): File jz.py, line 4, in ? print 'got %s\n' % _.group(1) NameError: name '_' is not

Re: regular expression: perl == python

2004-12-22 Thread JZ
Dnia Wed, 22 Dec 2004 10:27:39 +0100, Fredrik Lundh napisa(a): import re line = The food is under the bar in the barn. if re.search(r'foo(.*)bar',line): print 'got %s\n' % _.group(1) Traceback (most recent call last): File jz.py, line 4, in ? print 'got %s\n' % _.group(1)

Re: regular expression: perl == python

2004-12-22 Thread Jp Calderone
On Wed, 22 Dec 2004 16:44:46 +0100, JZ [EMAIL PROTECTED] wrote: Dnia Wed, 22 Dec 2004 10:27:39 +0100, Fredrik Lundh napisaƂ(a): import re line = The food is under the bar in the barn. if re.search(r'foo(.*)bar',line): print 'got %s\n' % _.group(1) Traceback (most recent call

Re: regular expression: perl == python

2004-12-22 Thread Fredrik Lundh
JZ wrote: import re line = The food is under the bar in the barn. if re.search(r'foo(.*)bar',line): print 'got %s\n' % _.group(1) Traceback (most recent call last): File jz.py, line 4, in ? print 'got %s\n' % _.group(1) NameError: name '_' is not defined I forgot to

Re: regular expression: perl == python

2004-12-22 Thread Nick Craig-Wood
1) In perl: $line = The food is under the bar in the barn.; if ( $line =~ /foo(.*)bar/ ) { print got $1\n; } in python, I don't know how I can do this? How does one capture the $1? (I know it is \1 but it is still not clear how I can simply print it. thanks Fredrik Lundh [EMAIL

Re: regular expression: perl == python

2004-12-22 Thread Fredrik Lundh
Nick Craig-Wood wrote: I've found that a slight irritation in python compared to perl - the fact that you need to create a match object (rather than relying on the silver thread of $_ (etc) running through your program ;-) the old regex engine associated the match with the pattern, but that

Re: regular expression: perl == python

2004-12-22 Thread John Machin
Fredrik Lundh wrote: JZ wrote: import re line = The food is under the bar in the barn. if re.search(r'foo(.*)bar',line): print 'got %s\n' % _.group(1) Traceback (most recent call last): File jz.py, line 4, in ? print 'got %s\n' % _.group(1) NameError: name '_'

Re: regular expression: perl == python

2004-12-22 Thread Fredrik Lundh
John Machin wrote: I forgot to add: I am using Python 2.3.4/Win32 (from ActiveState.com). The code works in my interpreter. only if you type it into the interactive prompt. see: No, it doesn't work at all, anywhere. Did you actually try this? the OP claims that it works in his

Re: regular expression: perl == python

2004-12-22 Thread John Machin
Fredrik Lundh wrote: John Machin wrote: I forgot to add: I am using Python 2.3.4/Win32 (from ActiveState.com). The code works in my interpreter. only if you type it into the interactive prompt. see: No, it doesn't work at all, anywhere. Did you actually try this? the OP

Re: regular expression: perl == python

2004-12-22 Thread Stephen Thorne
On 22 Dec 2004 17:30:04 GMT, Nick Craig-Wood [EMAIL PROTECTED] wrote: Is there an easy way round this? AFAIK you can't assign a variable in a compound statement, so you can't use elif at all here and hence the problem? I suppose you could use a monstrosity like this, which relies on the

regular expression: perl == python

2004-12-21 Thread les_ander
Hi, i am so use to perl's regular expression that i find it hard to memorize the functions in python; so i would appreciate if people can tell me some equivalents. 1) In perl: $line = The food is under the bar in the barn.; if ( $line =~ /foo(.*)bar/ ) { print got $1\n; } in python, I don't know

Re: regular expression: perl == python

2004-12-21 Thread Fredrik Lundh
[EMAIL PROTECTED] wrote: i am so use to perl's regular expression that i find it hard to memorize the functions in python; so i would appreciate if people can tell me some equivalents. 1) In perl: $line = The food is under the bar in the barn.; if ( $line =~ /foo(.*)bar/ ) { print got