regular expression: perl ==> python

Fredrik Lundh fredrik at pythonware.com
Wed Dec 22 10:55:55 EST 2004


"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 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:

    http://www.python.org/doc/2.4/tut/node5.html#SECTION005110000000000000000

    "In interactive mode, the last printed expression is assigned to the variable _.
    This means that when you are using Python as a desk calculator, it is some-
    what easier to continue calculations /.../"

the "_" symbol has no special meaning when you run a Python program, so the
"if re.search" construct won't work.

</F> 






More information about the Python-list mailing list