regular expression: perl ==> python

Jp Calderone exarkun at divmod.com
Wed Dec 22 10:51:24 EST 2004


On Wed, 22 Dec 2004 16:44:46 +0100, JZ <wnebfynj at mnovryyb.pbz> 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 last):
> >   File "jz.py", line 4, in ?
> >     print 'got %s\n' % _.group(1)
> > NameError: name '_' is not defined
> 
> I forgot to add: I am using Python 2.3.4/Win32 (from ActiveState.com). The
> code works in my interpreter.

  Note that _ is only automagically defined when you are using the 
interpreter interactively.  If you were to run this program on the 
command line, or invoke it any way other than interactively, it
breaks in the way Fred demonstrated.  You should avoid using _ 
in programs.  It is a convenience for interactive use only.

  Jp



More information about the Python-list mailing list