#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2004
    Posts
    30
    Rep Power
    0

    Regular expression (group)


    I know in php, you can get multi arrayed results with preg_match and preg_match_all.

    I was wondering if that was similar in the group() function in python by creating a list.

    Example, I have in the text:

    "mary had a little lamb, and a little rabbit."

    and I want to search for "little" and it's subsequent word and I want a list of the matches.

    So I'd have something like this:

    Code:
    content = 'mary had a little lamb, and a little rabbit.'
    required = '(little\s*?\w*?)\w'
    
    p = re.compile(required, re.IGNORECASE)
    
    gr = p.search(content)
    
    
    #This is the part I don't know what to do!!!
    print gr.groups(1)
    print gr.group(1)
    I want to create an output that would say:

    little lamb
    little rabbit

    and so on.

    How would I go about getting that?

    Thanks
    Last edited by duniyadnd; April 23rd, 2004 at 02:18 PM.
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2003
    Posts
    133
    Rep Power
    11
    Originally Posted by duniyadnd
    I want to create an output that would say:

    little lamb
    little rabbit

    and so on.

    How would I go about getting that?

    Thanks
    This code:
    Code:
    content = 'mary had a little lamb, and a little rabbit.'
    required = 'little\s+\w+(?=\W)'
    
    p = re.compile(required, re.IGNORECASE)
    gr = p.finditer(content)
    
    for match in gr:
        print match.group(0)
    outputs:
    Code:
    little lamb
    little rabbit
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2004
    Posts
    30
    Rep Power
    0
    aaaaaah, that worked, thanks.. t
  6. #4
  7. Hello World :)
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2003
    Location
    Hull, UK
    Posts
    2,537
    Rep Power
    69
    If you want to get a list instead of an itorator then use the findall() method instead, if you just want to loop over the values finditer() is fine but it lacks the flexability of a list in some respects... IMO of course

    Take care guys,

    Mark.
    programming language development: www.netytan.com Hula

  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2003
    Posts
    133
    Rep Power
    11
    Originally Posted by netytan
    If you want to get a list instead of an itorator then use the findall() method instead, if you just want to loop over the values finditer() is fine but it lacks the flexability of a list in some respects... IMO of course
    Mark.
    re.finditer is, of course, better not when you want to loop over the actual matches but when you want the actual match objects (with ability to get subgroups, find start and end of matches, etc.) instead of a plain list with text.
  10. #6
  11. Hello World :)
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2003
    Location
    Hull, UK
    Posts
    2,537
    Rep Power
    69
    Ah, but a list is much easier to manipulate than an itorator! Which is why i find it better since most of the time i'll find myself wanting to do something else with the values . But for looping i agree with perc of course

    Mark.
    programming language development: www.netytan.com Hula

  12. #7
  13. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2004
    Posts
    30
    Rep Power
    0
    That discussion helped even more.. Thanks guys..

IMN logo majestic logo threadwatch logo seochat tools logo