Name
findall
Synopsis
r.findall(s)When r has no groups,
findall returns a list of strings, each a
substring of s that is a non-overlapping
match with r. For example,
here’s how to print out all words in a file, one per
line:
import re
reword = re.compile(r'\w+')
for aword in reword.findall(open('afile.txt').read( )):
print awordWhen r has one group,
findall also returns a list of strings, but each
is the substring of s matching
r’s group. For example,
if you want to print only words that are followed by whitespace (not
punctuation), you need to change only one statement in the previous
example:
reword = re.compile('(\w+)\s')When r has n
groups (where n is greater than
1), findall returns a list of
tuples, one per non-overlapping match with
r. Each tuple has
n items, one per group of
r, the substring of
s matching the group. For example,
here’s how to print the first and last word of each
line that has at least two words:
import re
first_last = re.compile(r'^\W*(\w+)\b.*\b(\w+)\W*$',
re.MULTILINE)
for first, last in \
first_last.findall(open('afile.txt').read( )):
print first, last