Python

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

2017-08-31  本文已影响35人  一木之夏

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.

上一篇 下一篇

猜你喜欢

热点阅读