regular expression: perl ==> python

John Machin sjmachin at lexicon.net
Wed Dec 22 21:23:05 CET 2004


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 '_' 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:

No, it doesn't work at all, anywhere. Did you actually try this?

>
>
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 /.../"
>

In the 3 lines that are executed before the exception, there are *no*
printed expressions.

Python 2.4 (#60, Nov 30 2004, 11:49:19) [MSC v.1310 32 bit (Intel)] on
win32
Type "help", "copyright", "credits" or "license" for more information.
>>> 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 "<stdin>", line 2, in ?
NameError: name '_' is not defined
>>>




More information about the Python-list mailing list