python使用正则表达式替换字符串

sub()字符串替换

>>> a = '2012-12-12 13:12'

>>> p = re.compile(r'\D')

>>> p.sub('', a)

'201212121312'

>>> help(p.sub)

Help on built-in function sub:

sub(...)

sub(repl, string[, count = 0]) --> newstring

Return the string obtained by replacing the leftmost non-overlapping

occurrences of pattern in string by the replacement repl.


python使用正则表达式替换字符串_第1张图片

你可能感兴趣的:(python使用正则表达式替换字符串)