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
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
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
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
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)
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
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
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
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
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 '_'
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
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
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
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
[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
15 matches
Mail list logo