re - multiple results

P

Pingveno

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.
['AZBaCTR']

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

Thanks,
Pingveno
 
M

Michael Hoffman

Pingveno said:
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.

['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.
 
F

Fredrik Lundh

Pingveno said:
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.
['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>
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,768
Messages
2,569,575
Members
45,051
Latest member
CarleyMcCr

Latest Threads

Top