re - multiple results

Discussion in 'Python' started by Pingveno, Jun 10, 2005.

  1. Pingveno

    Pingveno Guest

    I'm working on the Python Challenge (make sure to try it:
    http://www.pythonchallenge.com). One of the puzzles requires the use of
    regular expressions, but with multiple matches in the text. I tried to
    use re.findall(), but I only go one result instead of a list of results.

    >>> print re.findall(r"myexpression",text)

    ['AZBaCTR']

    There should, of course, be several matches. What function should I use?
    Or is it not a function issue?

    Thanks,
    Pingveno
    Pingveno, Jun 10, 2005
    #1
    1. Advertising

  2. Pingveno wrote:
    > I'm working on the Python Challenge (make sure to try it:
    > http://www.pythonchallenge.com). One of the puzzles requires the use of
    > regular expressions, but with multiple matches in the text. I tried to
    > use re.findall(), but I only go one result instead of a list of results.
    >
    >>>>print re.findall(r"myexpression",text)

    >
    > ['AZBaCTR']
    >
    > Or is it not a function issue?


    Works for me. Although I usually prefer this idiom:

    re_something = re.compile(r"pattern")
    re_something.findall(text)

    You're doing something else wrong.
    --
    Michael Hoffman
    Michael Hoffman, Jun 11, 2005
    #2
    1. Advertising

  3. Pingveno wrote:

    > I'm working on the Python Challenge (make sure to try it:
    > http://www.pythonchallenge.com). One of the puzzles requires the use of
    > regular expressions, but with multiple matches in the text. I tried to
    > use re.findall(), but I only go one result instead of a list of results.
    >
    >>>> print re.findall(r"myexpression",text)

    > ['AZBaCTR']
    >
    > There should, of course, be several matches.


    "myexpression" won't return "AZBaCTR" for any kind of input, so I'm not
    sure what "of course" really refers to...

    > What function should I use? Or is it not a function issue?


    my guess is that you're using "*" or "+" in a situation where you don't really
    need them. cf.

    >>> re.findall("\w", "abcdef")

    ['a', 'b', 'c', 'd', 'e', 'f']
    >>> re.findall("\w+", "abcdef")

    ['abcdef']

    </F>
    Fredrik Lundh, Jun 13, 2005
    #3
    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. Patrick Olurotimi Ige
    Replies:
    2
    Views:
    507
    Eliyahu Goldin
    May 9, 2005
  2. Monique Y. Mudama
    Replies:
    1
    Views:
    448
    Monique Y. Mudama
    Jun 28, 2005
  3. lovecreatesbeauty
    Replies:
    8
    Views:
    1,636
    Old Wolf
    Sep 12, 2005
  4. Ken Fine
    Replies:
    3
    Views:
    528
    Steven Cheng [MSFT]
    Jul 23, 2008
  5. Biranchi Narayan Panda
    Replies:
    0
    Views:
    542
    Biranchi Narayan Panda
    Feb 21, 2010
Loading...

Share This Page