re Module Phyton provides other great methods, that is not provided by Perl and Java. If we want to find all substring in string that is suitable with regular expression. We have to use loop and Perl and other language.

while ($string =- m/regrex/g) {
	print "Found '$&'. Next attempt at character ". pos($string)+1. "\\n";
}

Untitled

import re

re.findall(pattern, string[, flags])

findall returns all possible match patterns that is not overlapped in string, as list string. String is scanned from left to right, and the match is returned according to order when it finds.

Example:

import re

t= "A fat cat doesn't eat oat but a rat eats bats."
mo = re.findall("[force]at", t)
print(mo)
['fat', 'cat', 'eat', 'oat', 'rat', 'eat']

If one or more group is in the pattern, findall returns group list. This will be tuple list if the pattern has more than a group. We show this in the next example. We have a set of long with various Python courses and the date. With first call to findall, we don’t use any grouping and as the result receive complete string. In the next call, we use grouping and findall returns list of 2-tupel, where each of it has special name as first component and date as second component:

import re
courses = '''Pyhton Training Course for 
Beginners: 15/Aug/2011 - 19/Aug/2011; 
Python Training Course Intermediate: 12/Dec/2011 - 16/Dec/2011;
Pyhton Text Processing Course: 31/Oct/2011 - 4/Nov/2011'''
items = re.findall("[^:]*:[^;]*;?", courses)
items
['Pyhton Training Course for \\nBeginners: 15/Aug/2011 - 19/Aug/2011;',
 ' \\nPython Training Course Intermediate: 12/Dec/2011 - 16/Dec/2011;',
 '\\nPyhton Text Processing Course: 31/Oct/2011 - 4/Nov/2011']