help in converting perl re to python re

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

    #1

    help in converting perl re to python re

    hi

    i have some regular exp code in perl that i want to convert to python.


    if $line =~ m#<(tag1)>(.*)</\1>#
    {
    $variable = $2;
    }

    the perl code finds a line that matches something like

    "<tag1>sometext <\tag1>" in the line and then assign $variable the value
    of "sometext"

    how can i do an equivalent of that using re module?
    thanks

  • Joel Hedlund

    #2
    Re: help in converting perl re to python re

    Hi

    [color=blue]
    > the perl code finds a line that matches something like
    > "<tag1>sometext <\tag1>" in the line and then assign $variable the value
    > of "sometext"[/color]

    No, but if you use a closing </tag1> instead of <\tag1> it does. You had me scratching my head for a while there. :-)

    This should do it in python:

    ------------------------------------------------
    #!/usr/bin/python

    import re

    regexp = re.compile(r"<( tag1)>(.*)</\1>")
    line = "<tag1>sometext </tag1>"
    match = regexp.search(l ine)

    if match:
    variable = match.group(2)

    ------------------------------------------------

    Good luck!
    /Joel Hedlund


    eight02645999@y ahoo.com wrote:[color=blue]
    > hi
    >
    > i have some regular exp code in perl that i want to convert to python.
    >
    >
    > if $line =~ m#<(tag1)>(.*)</\1>#
    > {
    > $variable = $2;
    > }
    >
    > the perl code finds a line that matches something like
    >
    > "<tag1>sometext <\tag1>" in the line and then assign $variable the value
    > of "sometext"
    >
    > how can i do an equivalent of that using re module?
    > thanks
    >[/color]

    Comment

    • Sybren Stuvel

      #3
      Re: help in converting perl re to python re

      Joel Hedlund enlightened us with:[color=blue]
      > regexp = re.compile(r"<( tag1)>(.*)</\1>")[/color]

      I'd go for

      regexp = re.compile(r"<( tag1)>(.*?)</\1>")

      Otherwise this:

      line = "<tag1>sometext </tag1><tag1>othe rtext</tag1>"
      match = regexp.search(l ine)

      will result in 'sometext</tag1><tag1>othe rtext'

      Sybren
      --
      The problem with the world is stupidity. Not saying there should be a
      capital punishment for stupidity, but why don't we just take the
      safety labels off of everything and let the problem solve itself?
      Frank Zappa

      Comment

      • Joel Hedlund

        #4
        Re: help in converting perl re to python re

        > I'd go for[color=blue]
        > regexp = re.compile(r"<( tag1)>(.*?)</\1>")[/color]

        Indeed. I second that.

        /Joel

        Comment

        • Mitja Trampus

          #5
          Re: help in converting perl re to python re

          >> i have some regular exp code in perl that i want to convert to python.[color=blue][color=green]
          >> if $line =~ m#<(tag1)>(.*)</\1>#
          >> {
          >> $variable = $2;
          >> }[/color][/color]
          [color=blue]
          > regexp = re.compile(r"<( tag1)>(.*)</\1>")
          > line = "<tag1>sometext </tag1>"
          > match = regexp.search(l ine)
          > if match:
          > variable = match.group(2)[/color]

          Or, if you prefer shorter, quick-and-dirty syntax closer to perl's approach:

          match = re.search(r"<(t ag1)>(.*)</\1>", line)
          if match: variable = match.group(2)

          Comment

          Working...