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 t
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 sre.
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
>
>
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 t
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
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
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'
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 th
> 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 <[E
Dnia Wed, 22 Dec 2004 16:55:55 +0100, Fredrik Lundh napisał(a):
> the "_" symbol has no special meaning when you run a Python program,
That's right. So the final code will be:
import re
line = "The food is under the bar in the barn."
found = re.search('foo(.*)bar',line)
if found: print 'got %s
"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 n
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 (m
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' % _.
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
"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 defined
Dnia 21 Dec 2004 21:12:09 -0800, [EMAIL PROTECTED] napisał(a):
> 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 no
<[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/ ) { prin
[EMAIL PROTECTED] wrote:
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?
I don't know Perl very well, but I believe this is more or less the
equivalent:
>>> import re
>>> line = "The food
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
19 matches
Mail list logo