How to use python regular expression to substitute string value

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Allerdyce.John@gmail.com

    #1

    How to use python regular expression to substitute string value

    Hi,
    I am new to python. I would like to know how to use python regular
    expression to substitute string value?
    I have an input string like this:
    x:11 y:0 w:760 h:19 area:14440 areaPerCent:0
    totalAreaPerCen t:-3.08011e+16 type:3 path:///-/1/1

    and I would like to convert it to:
    rect x="11" y="0" width="760" height="14440"

    all I care about the input string is x, y, w, h.

    Thank you for any pointers.

  • Pasi Savolainen

    #2
    Re: How to use python regular expression to substitute string value

    Allerdyce.John@ gmail.com writes:
    [color=blue]
    > Hi,
    > I am new to python. I would like to know how to use python regular
    > expression to substitute string value?
    > I have an input string like this:
    > x:11 y:0 w:760 h:19 area:14440 areaPerCent:0
    > totalAreaPerCen t:-3.08011e+16 type:3 path:///-/1/1
    >
    > and I would like to convert it to:
    > rect x="11" y="0" width="760" height="14440"
    >
    > all I care about the input string is x, y, w, h.[/color]

    I'd say you're better off with 'findall':
    - -
    import re
    line = "x:11 y:0 w:760 h:19 area:14440 areaPerCent:0 totalAreaPerCen t:-3.08011e+16 type:3 path:///-/1/1"
    pattern = "x:(\d+) y:(\d+) w:(\d+) h:(\d+) area:(\d+)"
    x, y, width, height, area = re.findall(patt ern, line)[0]
    print "rect x=\"%(x)s\" y=\"%(y)s\" width=\"%(width )s\" height=\"%(heig ht)s\" ..." % locals()
    - -

    x .. area are strings when they come out of findall, so you may want to:
    - -
    x .. area = map (lambda x: int(x), re.findall (pattern, line)[0]
    - -

    --
    Psi -- <http://www.iki.fi/pasi.savolainen >

    Comment

    • Allerdyce.John@gmail.com

      #3
      Re: How to use python regular expression to substitute string value

      Thanks. But i don't understand why I need to do this:
      x. . area = map (lambda x: int(x), re.findall (pattern, line)[0]

      if i have the value already by doing this:
      x, y, width, height, area = re.findall(patt ern, line)[0]
      print "rect x=\"%(x)s\" y=\"%(y)s\" width=\"%(width )s\"
      height=\"%(heig ht)s\" ..." % locals()

      Comment

      • Crutcher

        #4
        Re: How to use python regular expression to substitute string value

        You don't really need regexes for this.

        Assuming there is no whitespace in any of your values, it should be
        really easy to parse the string.

        s = 'x:11 y:0 w:760 h:19 area:14440 areaPerCent:0
        totalAreaPerCen t:-3.08011e+16 type:3 path:///-/1/1'

        s.split() # break the string on whitespace[color=blue]
        > ['x:11', 'y:0', 'w:760', 'h:19', 'area:14440', 'areaPerCent:0' , 'totalAreaPerCe nt:-3.08011e+16', 'type:3', 'path:///-/1/1'][/color]

        [ t.split(':',1) for t in s.split() ] # break each term on the first
        ':'[color=blue]
        > [['x', '11'], ['y', '0'], ['w', '760'], ['h', '19'], ['area', '14440'], ['areaPerCent', '0'], ['totalAreaPerCe nt', '-3.08011e+16'], ['type', '3'], ['path', '///-/1/1']][/color]

        d = dict([ t.split(':',1) for t in s.split() ]) # make a dict of the
        list[color=blue]
        > {'type': '3', 'area': '14440', 'h': '19', 'w': '760', 'areaPerCent': '0', 'y': '0', 'x': '11', 'path': '///-/1/1', 'totalAreaPerCe nt': '-3.08011e+16'}[/color]

        Now you can do this:
        new_s = 'rect x="%(x)s" y="%(y)s" width="%(w)s" height="%(h)s"' % d[color=blue]
        > 'rect x="11" y="0" width="760" height="19"'[/color]

        Comment

        • Allerdyce.John@gmail.com

          #5
          Re: How to use python regular expression to substitute string value

          okay, but I have a simpler question, how can I split using only "\n"?

          I try this:
          strings = node.data.split ("\n");
          print node.data

          for str in strings:
          print str

          where node.data has multiple lines, but in the for loop, I don't see
          str gets print out.

          Comment

          • Crutcher

            #6
            Re: How to use python regular expression to substitute string value

            Are you sure node.data contains newlines?
            You could try just:
            print node.data
            print node.data.split ('\n')

            This should give you an idea. From the interpreter:[color=blue][color=green][color=darkred]
            >>> s = """[/color][/color][/color]
            .... abc
            .... def
            .... xyz"""[color=blue][color=green][color=darkred]
            >>> s.split('\n')[/color][/color][/color]
            ['', 'abc', 'def', 'xyz']

            Comment

            • Allerdyce.John@gmail.com

              #7
              Re: How to use python regular expression to substitute string value

              When I try your idea, I have this error

              x, y, width, height = re.findall(patt ern, str)[0]
              IndexError: list index out of range

              How can I use findall to handle error case? i.e. what if there is no
              match? how can I handle it gracefully?

              Comment

              • Pasi Savolainen

                #8
                Re: How to use python regular expression to substitute string value

                Allerdyce.John@ gmail.com writes:
                [color=blue]
                > When I try your idea, I have this error
                >
                > x, y, width, height = re.findall(patt ern, str)[0]
                > IndexError: list index out of range
                >
                > How can I use findall to handle error case? i.e. what if there is no
                > match? how can I handle it gracefully?[/color]

                This is explained in python docs, and do try them in interactive python,
                for example IPython is great for this stuff.

                - -
                ipython ->

                In [1]:import re

                In [2]:line = "foobar"

                In [3]:re.findall ("baz", line)
                Out[3]:[]

                In [4]:re.findall ("o", line)
                Out[4]:['o', 'o']
                - -

                So you can do:
                - -
                match = re.findall(patt ern, str)
                if match:
                x, y, width, height = match[0]
                ...
                else:
                # no match
                - -



                --
                Psi -- <http://www.iki.fi/pasi.savolainen >

                Comment

                Working...