Results 1 to 5 of 5

Thread: Python, re and match objects

  1. #1
    Join Date
    Jan 2006
    Location
    Leuven, Belgium
    Beans
    3,414

    Python, re and match objects

    I'm trying to get to know python a bit, but I'm kind of stuck...
    How do the regular expressions work??? I know how to define and compile a re and I use the search function to go through a string with it. But then I get a "Match object" returned and I can't find a way to do anything useful with that... How can I restore the found occurrences or the their positions in the input string?

    I need to find the matching sequences in a string, can someone point me how to do so?
    This is what I've got now:
    Code:
    p=re.compile("[A-Z][A-Z][A-Z][a-z][A-Z][A-Z][A-Z]")
    m=re.search(p,str)

  2. #2
    Join Date
    Jun 2006
    Location
    CT, USA
    Beans
    5,267
    Distro
    Ubuntu 6.10 Edgy

    Re: Python, re and match objects

    Quote Originally Posted by Ramses de Norre View Post
    I need to find the matching sequences in a string, can someone point me how to do so?
    Unless you need really complicated regex pattern tricks, much of finding can be done with string methods: find, rfind, index, startswith, split and join. Proper regular expressions are very powerful, but hard. I try to stay with string methods if I can. If not, I just look for examples. http://regexlib.com/ has many examples and regex tester.

    Regular expressions is tricky programming language by itself (beware of greedy dot-star) - http://en.wikipedia.org/wiki/Regular_expression is good start, see links/articles at the end.

    http://kodos.sourceforge.net/ is your own regex tester.

  3. #3
    WW is offline Iced Blended Vanilla Crème Ubuntu
    Join Date
    Oct 2004
    Beans
    1,532

    Re: Python, re and match objects

    Maybe findall?

    testfindall.py
    Code:
    import re
    
    p=re.compile("[A-Z][A-Z][A-Z][a-z][A-Z][A-Z][A-Z]")
    str1 = "AAAAAbCCCCdEEEEfGGGhhhIIII"
    m1 = p.findall(str1)
    print str1, " contains ", m1
    str2 = "AAAbCCCdEEEfGGGhIIIjKKKlMMM"
    m2 = p.findall(str2)
    print str2, " contains ", m2
    Code:
    $ python testfindall.py
    AAAAAbCCCCdEEEEfGGGhhhIIII  contains  ['AAAbCCC', 'EEEfGGG']
    AAAbCCCdEEEfGGGhIIIjKKKlMMM  contains  ['AAAbCCC', 'EEEfGGG', 'IIIjKKK']
    $
    (Check out section 4.2.3 of the Python re docs.)

  4. #4
    Join Date
    Sep 2006
    Beans
    2,914

    Re: Python, re and match objects

    Quote Originally Posted by Ramses de Norre View Post
    I need to find the matching sequences in a string, can someone point me how to do so?
    This is what I've got now:
    Code:
    p=re.compile("[A-Z][A-Z][A-Z][a-z][A-Z][A-Z][A-Z]")
    m=re.search(p,str)
    once you compiled your regexp using p=re.compile(..), you can use the re module methods like finditer or findall...check the re module docs for more info.
    Code:
    p.findall(str)

  5. #5
    Join Date
    Jan 2006
    Location
    Leuven, Belgium
    Beans
    3,414

    Re: Python, re and match objects

    findall was exactly what I was looking for!
    Strange that I couldn't find it on the web...

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •