re.findall

Discussion in 'Python' started by Will Stuyvesant, Jul 16, 2004.

  1. A question about the findall function in the re module, and I also
    would be happy with pointers to online documentation with which I
    could have found a solution myself (if it even exists!).

    I have this program and result:

    -------------- program ------------------
    import re

    pat = r'(.*) or (.*)'
    r = re.findall(pat, "i or j or k")
    print r
    -------------- result -------------------
    [('i or j', 'k')]
    -------------------------------------------

    But the result I want is this:
    [('i', 'j or k'), ('i or j', 'k')]

    So my regular expression is wrong, but also variations like
    r'^(.*|.*?) or (.*|.*?)$' do not work. I have the feeling I am
    overlooking something simple. Maybe I should not try to use
    re.findall to get the desired result, but do a re.match in a
    while-loop?

    Stated in English the problem is this: give me all combinations of
    strings left and right of r' or ', and return them as a list of
    tuples. Sure I can solve this without using the re module, using
    while-loops and string.search etc., but I also have variations of
    this problem and learning re seems useful.
    Will Stuyvesant, Jul 16, 2004
    #1
    1. Advertising

  2. (Will Stuyvesant) wrote in
    news::

    > A question about the findall function in the re module, and I also
    > would be happy with pointers to online documentation with which I
    > could have found a solution myself (if it even exists!).
    >


    The problem is that the strings you want to find are overlapping.
    This should get you started:

    import re

    s = "i or j or k or grr"
    pat = re.compile(r'\w+ or \w+')

    startposition = 0
    while 1:
    res = pat.search(s, startposition)
    if res == None:
    break
    startposition = res.start() + 1
    print res.group()


    Matthias
    Matthias Huening, Jul 16, 2004
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?Utf-8?B?bWc=?=

    Active Directory problem - search.FindAll()

    =?Utf-8?B?bWc=?=, Feb 22, 2004, in forum: ASP .Net
    Replies:
    0
    Views:
    2,365
    =?Utf-8?B?bWc=?=
    Feb 22, 2004
  2. =?Utf-8?B?QW5kcmV3MTI0?=

    DirectorySearcher.FindAll()

    =?Utf-8?B?QW5kcmV3MTI0?=, Jul 11, 2004, in forum: ASP .Net
    Replies:
    0
    Views:
    2,299
    =?Utf-8?B?QW5kcmV3MTI0?=
    Jul 11, 2004
  3. Ezequiel, Justin

    optional arguments to pattern.findall()

    Ezequiel, Justin, May 5, 2004, in forum: Python
    Replies:
    1
    Views:
    331
    Peter Otten
    May 5, 2004
  4. gohaku

    [import re] match or findall?

    gohaku, Jul 3, 2004, in forum: Python
    Replies:
    1
    Views:
    289
    Sam Holden
    Jul 4, 2004
  5. could ildg
    Replies:
    0
    Views:
    292
    could ildg
    Apr 7, 2005
Loading...

Share This Page