March 2003
Intermediate to advanced
656 pages
39h 30m
English
subn
r.subn(repl,s,count=0)
subn is the same as sub, except
that subn returns a pair
(
new_string,
n
) where
n is the number of substitutions that
subn has performed. For example, to count the
number of occurrences of substring 'hello' in any
mix of cases, one way is:
import re
rehello = re.compile(r'hello', re.IGNORECASE)
junk, count = rehello.subn('', astring)
print 'Found', count, 'occurrences of "hello"'