regular expression: perl ==> python

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • les_ander@yahoo.com

    #1

    regular expression: perl ==> python

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

  • Steven Bethard

    #2
    Re: regular expression: perl ==&gt; python

    les_ander@yahoo .com wrote:[color=blue]
    >
    > 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?[/color]

    I don't know Perl very well, but I believe this is more or less the
    equivalent:
    [color=blue][color=green][color=darkred]
    >>> import re
    >>> line = "The food is under the bar in the barn."
    >>> matcher = re.compile(r'fo o(.*)bar')
    >>> match = matcher.search( line)
    >>> print 'got <%s>' % match.group(1)[/color][/color][/color]
    got <d is under the bar in the >

    Of course, you can do this in fewer lines if you like:
    [color=blue][color=green][color=darkred]
    >>> print 'got <%s>' % re.search(r'foo (.*bar)', line).group(1)[/color][/color][/color]
    got <d is under the bar in the bar>

    Steve

    Comment

    • Fredrik Lundh

      #3
      Re: regular expression: perl ==&gt; python

      <les_ander@yaho o.com> wrote:
      [color=blue]
      > 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 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.[/color]

      in Python, the RE machinery returns match objects, which has methods
      that let you dig out more information about the match. "captured groups"
      are available via the "group" method:

      m = re.search(..., line)
      if m:
      print "got", m.group(1)

      see the regex howto (or the RE chapter in the library reference) for more
      information:



      </F>



      Comment

      • JZ

        #4
        Re: regular expression: perl ==&gt; python

        Dnia 21 Dec 2004 21:12:09 -0800, les_ander@yahoo .com napisa³(a):
        [color=blue]
        > 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[/color]

        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)

        --
        JZ ICQ:6712522

        Comment

        • Fredrik Lundh

          #5
          Re: regular expression: perl ==&gt; python

          "JZ" <wnebfynj@mnovr yyb.pbz> wrote:
          [color=blue]
          > 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)[/color]

          Traceback (most recent call last):
          File "jz.py", line 4, in ?
          print 'got %s\n' % _.group(1)
          NameError: name '_' is not defined

          </F>



          Comment

          • Doug Holton

            #6
            Re: regular expression: perl ==&gt; python

            Fredrik Lundh wrote:[color=blue]
            > "JZ" <wnebfynj@mnovr yyb.pbz> wrote:
            >
            >[color=green]
            >>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)[/color]
            >
            >
            > Traceback (most recent call last):
            > File "jz.py", line 4, in ?
            > print 'got %s\n' % _.group(1)
            > NameError: name '_' is not defined[/color]

            He was using the python interactive prompt, which I suspect you already
            knew.

            Comment

            • JZ

              #7
              Re: regular expression: perl ==&gt; python

              Dnia Wed, 22 Dec 2004 10:27:39 +0100, Fredrik Lundh napisa³(a):
              [color=blue][color=green]
              >> 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)[/color]
              >
              > Traceback (most recent call last):
              > File "jz.py", line 4, in ?
              > print 'got %s\n' % _.group(1)
              > NameError: name '_' is not defined[/color]

              I forgot to add: I am using Python 2.3.4/Win32 (from ActiveState.com ). The
              code works in my interpreter.

              --
              JZ

              Comment

              • Fredrik Lundh

                #8
                Re: regular expression: perl ==&gt; python

                "JZ" wrote:
                [color=blue][color=green][color=darkred]
                > >> 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)[/color]
                > >
                > > Traceback (most recent call last):
                > > File "jz.py", line 4, in ?
                > > print 'got %s\n' % _.group(1)
                > > NameError: name '_' is not defined[/color]
                >
                > I forgot to add: I am using Python 2.3.4/Win32 (from ActiveState.com ). The
                > code works in my interpreter.[/color]

                only if you type it into the interactive prompt. see:



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



                Comment

                • JZ

                  #9
                  Re: regular expression: perl ==&gt; python

                  Dnia Wed, 22 Dec 2004 16:55:55 +0100, Fredrik Lundh napisa³(a):
                  [color=blue]
                  > the "_" symbol has no special meaning when you run a Python program,[/color]

                  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\n' % found.group(1)

                  --
                  JZ ICQ:6712522

                  Comment

                  • Nick Craig-Wood

                    #10
                    Re: regular expression: perl ==&gt; python

                    > 1) In perl:[color=blue]
                    > $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[/color]


                    Fredrik Lundh <fredrik@python ware.com> wrote:[color=blue]
                    > "JZ" <wnebfynj@mnovr yyb.pbz> wrote:
                    >[color=green]
                    > > 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)[/color]
                    >
                    > Traceback (most recent call last):
                    > File "jz.py", line 4, in ?
                    > print 'got %s\n' % _.group(1)
                    > NameError: name '_' is not defined[/color]

                    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 ;-)

                    import re
                    line = "The food is under the bar in the barn."
                    m = re.search(r'foo (.*)bar',line)
                    if m:
                    print 'got %s\n' % m.group(1)

                    This becomes particularly irritating when using if, elif etc, to
                    match a series of regexps, eg

                    line = "123123"
                    m = re.search(r'^(\ d+)$', line)
                    if m:
                    print "int",int(m.gro up(1))
                    else:
                    m = re.search(r'^(\ d*\.\d*)$', line)
                    if m:
                    print "float",float(m .group(1))
                    else:
                    print "unknown thing", line

                    The indentation keeps growing which looks rather untidy compared to
                    the perl

                    $line = "123123";
                    if ($line =~ /^(\d+)$/) {
                    print "int $1\n";
                    }
                    elsif ($line =~ /^(\d*\.\d*)$/) {
                    print "float $1\n";
                    }
                    else {
                    print "unknown thing $line\n";
                    }

                    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
                    fact that list.append() returns None...

                    line = "123123"
                    m = []
                    if m.append(re.sea rch(r'^(\d+)$', line)) or m[-1]:
                    print "int",int(m[-1].group(1))
                    elif m.append(re.sea rch(r'^(\d*\.\d *)$', line)) or m[-1]:
                    print "float",flo at(m[-1].group(1))
                    else:
                    print "unknown thing", line

                    --
                    Nick Craig-Wood <nick@craig-wood.com> -- http://www.craig-wood.com/nick

                    Comment

                    • Fredrik Lundh

                      #11
                      Re: regular expression: perl ==&gt; python

                      Nick Craig-Wood wrote:
                      [color=blue]
                      > 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 ;-)[/color]

                      the old "regex" engine associated the match with the pattern, but that
                      approach isn't thread safe...
                      [color=blue]
                      > line = "123123"
                      > m = re.search(r'^(\ d+)$', line)
                      > if m:
                      > print "int",int(m.gro up(1))
                      > else:
                      > m = re.search(r'^(\ d*\.\d*)$', line)
                      > if m:
                      > print "float",float(m .group(1))
                      > else:
                      > print "unknown thing", line[/color]

                      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. see



                      for more on this topic.

                      </F>



                      Comment

                      • John Machin

                        #12
                        Re: regular expression: perl ==&gt; python


                        Fredrik Lundh wrote:[color=blue]
                        > "JZ" wrote:
                        >[color=green][color=darkred]
                        > > >> 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[/color]
                        > >
                        > > I forgot to add: I am using Python 2.3.4/Win32 (from[/color][/color]
                        ActiveState.com ). The[color=blue][color=green]
                        > > code works in my interpreter.[/color]
                        >
                        > only if you type it into the interactive prompt. see:[/color]

                        No, it doesn't work at all, anywhere. Did you actually try this?
                        [color=blue]
                        >
                        >[/color]
                        http://www.python.org/doc/2.4/tut/no...00000000000000[color=blue]
                        >
                        > "In interactive mode, the last printed expression is assigned to[/color]
                        the variable _.[color=blue]
                        > This means that when you are using Python as a desk calculator,[/color]
                        it is some-[color=blue]
                        > what easier to continue calculations /.../"
                        >[/color]

                        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.[color=blue][color=green][color=darkred]
                        >>> import re
                        >>> line = "The food is under the bar in the barn."
                        >>> if re.search(r'foo (.*)bar',line):[/color][/color][/color]
                        .... print 'got %s\n' % _.group(1)
                        ....
                        Traceback (most recent call last):
                        File "<stdin>", line 2, in ?
                        NameError: name '_' is not defined[color=blue][color=green][color=darkred]
                        >>>[/color][/color][/color]

                        Comment

                        • Fredrik Lundh

                          #13
                          Re: regular expression: perl ==&gt; python

                          John Machin wrote:[color=blue]
                          >[color=green][color=darkred]
                          >> > I forgot to add: I am using Python 2.3.4/Win32 (from ActiveState.com ). The
                          >> > code works in my interpreter.[/color]
                          >>
                          >> only if you type it into the interactive prompt. see:[/color]
                          >
                          > No, it doesn't work at all, anywhere. Did you actually try this?[/color]

                          the OP claims that it works in his ActiveState install (PythonWin?). maybe he
                          played with re.search before typing in the commands he quoted; maybe Python-
                          Win contains some extra hacks?

                          as I've illustrated earlier, it definitely doesn't work in a script executed by a standard
                          Python...

                          </F>



                          Comment

                          • John Machin

                            #14
                            Re: regular expression: perl ==&gt; python


                            Fredrik Lundh wrote:[color=blue]
                            > John Machin wrote:[color=green]
                            > >[color=darkred]
                            > >> > I forgot to add: I am using Python 2.3.4/Win32 (from[/color][/color][/color]
                            ActiveState.com ). The[color=blue][color=green][color=darkred]
                            > >> > code works in my interpreter.
                            > >>
                            > >> only if you type it into the interactive prompt. see:[/color]
                            > >
                            > > No, it doesn't work at all, anywhere. Did you actually try this?[/color]
                            >
                            > the OP claims that it works in his ActiveState install (PythonWin?).[/color]
                            maybe he[color=blue]
                            > played with re.search before typing in the commands he quoted; maybe[/color]
                            Python-[color=blue]
                            > Win contains some extra hacks?
                            >
                            > as I've illustrated earlier, it definitely doesn't work in a script[/color]
                            executed by a standard[color=blue]
                            > Python...
                            >
                            > </F>[/color]

                            It is quite possible that the OP played with re.search before before
                            typing in the commands he quoted; however *you* claimed that it [his
                            quoted commands] worked "only if you type it into the interactive
                            prompt". It doesn't work, in the unqualified sense that I understood.

                            Anyway, enough of punch-ups about how many dunces can angle on the hat
                            of a pun -- I did appreciate your other posting about multiple patterns
                            and "lastindex" ; thanks.

                            Comment

                            • Stephen Thorne

                              #15
                              Re: regular expression: perl ==&gt; python

                              On 22 Dec 2004 17:30:04 GMT, Nick Craig-Wood <nick@craig-wood.com> wrote:[color=blue]
                              > 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
                              > fact that list.append() returns None...
                              >
                              > line = "123123"
                              > m = []
                              > if m.append(re.sea rch(r'^(\d+)$', line)) or m[-1]:
                              > print "int",int(m[-1].group(1))
                              > elif m.append(re.sea rch(r'^(\d*\.\d *)$', line)) or m[-1]:
                              > print "float",flo at(m[-1].group(1))
                              > else:
                              > print "unknown thing", line[/color]

                              I wrote a scanner for a recursive decent parser a while back. This is
                              the pattern i used for using mulitple regexps, instead of using an
                              if/elif/else chain.

                              import re
                              patterns = [
                              (re.compile('^( \d+)$'),int),
                              (re.compile('^( \d+\.\d*)$'),fl oat),
                              ]

                              def convert(s):
                              for regexp, action in patterns:
                              m = regexp.match(s)
                              if not m:
                              continue
                              return action(m.group( 1))
                              raise ValueError, "Invalid input %r, was not a numeric string" % (s,)

                              if __name__ == '__main__':
                              tests = [ ("123123",12312 3), ("123.123",123. 123), ("123.",123. ) ]
                              for input, expected in tests:
                              assert convert(input) == expected

                              try:
                              convert('')
                              convert('abc')
                              except:
                              pass
                              else:
                              assert None,"Should Raise on invalid input"


                              Of course, I wrote the tests first. I used your regexp's but I was
                              confused as to why you were always using .group(1), but decided to
                              leave it. I would probably actually send the entire match object to
                              the action. Using something like:
                              (re.compile('^( \d+)$'),lambda m:int(m.group(1 )),
                              and
                              return action(m)

                              but lambdas are going out fashion. :(

                              Stephen Thorne

                              Comment

                              Working...