regular expression match collection

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

    #1

    regular expression match collection

    Hello,

    For example I have a string : "Halo by by by"
    Then I want to take and know the possition of every "by"
    how can I do it in python?

    I tried to use:

    p = re.compile(r"by ")
    m = p.search("Helo by by by")
    print m.group() # result "by"
    print m.span() # result (5,7)

    How can I get the information of the other by ?

    Sincerely Yours,
    Pujo Aji

  • Fredrik Lundh

    #2
    Re: regular expression match collection

    <ajikoe@gmail.c om> wrote:
    [color=blue]
    > For example I have a string : "Halo by by by"
    > Then I want to take and know the possition of every "by"
    > how can I do it in python?
    >
    > I tried to use:
    >
    > p = re.compile(r"by ")
    > m = p.search("Helo by by by")
    > print m.group() # result "by"
    > print m.span() # result (5,7)
    >
    > How can I get the information of the other by ?[/color]
    [color=blue][color=green][color=darkred]
    >>> import re
    >>> p = re.compile("by" )
    >>> for m in p.finditer("Hel o by by by"):[/color][/color][/color]
    .... print m.span()
    ....
    (5, 7)
    (8, 10)
    (11, 13)

    </F>



    Comment

    • Steve Holden

      #3
      Re: regular expression match collection

      ajikoe@gmail.co m wrote:
      [color=blue]
      > Hello,
      >
      > For example I have a string : "Halo by by by"
      > Then I want to take and know the possition of every "by"
      > how can I do it in python?
      >
      > I tried to use:
      >
      > p = re.compile(r"by ")
      > m = p.search("Helo by by by")
      > print m.group() # result "by"
      > print m.span() # result (5,7)
      >
      > How can I get the information of the other by ?
      >
      > Sincerely Yours,
      > Pujo Aji
      >[/color]
      You need re.findall() (or, equivalently, the findall() method of an re).

      regards
      Steve
      --
      Meet the Python developers and your c.l.py favorites March 23-25
      Come to PyCon DC 2005 http://www.pycon.org/
      Steve Holden http://www.holdenweb.com/

      Comment

      • P@draigBrady.com

        #4
        Re: regular expression match collection

        ajikoe@gmail.co m wrote:[color=blue]
        > Hello,
        >
        > For example I have a string : "Halo by by by"
        > Then I want to take and know the possition of every "by"
        > how can I do it in python?[/color]

        [ match.start() for match in p.finditer("Hel o by by by") ]

        see:


        --
        Pádraig Brady - http://www.pixelbeat.org
        --

        Comment

        • ajikoe@gmail.com

          #5
          Re: regular expression match collection

          Thanks you...

          Comment

          Working...