>>> help(re.sub)
1 Help on function sub in module re:
2
3 sub(pattern, repl, string, count=0)
4 Return the string obtained by replacing the leftmost
5 non-overlapping occurrences of the pattern in string by the
6 replacement repl. repl can be either a string or a callable;
7 if a callable, it's passed the match object and must return
8 a replacement string to be used.
В re.sub
отсутствует параметр функции для флагов регулярных выражений (IGNORECASE, MULTILINE, DOTALL
), как в re.compile
.
Альтернативы:
>>> re.sub("[M|m]r", "", "Mr Bean")
' Bean'
>>> re.sub("(?i)mr", "", "Mr Bean")
' Bean'
Редактировать В Python 3.1 добавлена поддержка флагов регулярных выражений, http://docs.python.org/3.1/whatsnew/3.1.html. С 3.1 подпись, например, например. re.sub
выглядит так:
re.sub(pattern, repl, string[, count, flags])