re.sub

re.sub的两种用法
2009-04-25 1:00
sub(pattern, repl, string, count=0)
    Return the string obtained by replacing the leftmost
    non-overlapping occurrences of the pattern in string by the
    replacement repl. repl can be either a string or a callable;
    if a callable, it's passed the match object and must return
    a replacement string to be used.
先来个简单的
>>> alist
'12345678903239892323982392398789'
>>> re.sub(r'23', '-', alist)
'1-45678903-989--98-9-98789'

再来个复杂点的,注意上面红色的字。
Google网页的中文字符都是用unicode来表示的,
下面用一个丑陋的方法把它转出来。
>>> c
'艾莉西亚 凯斯(Alicia Keys)\n- 谷&#274
68;音乐搜索'
>>> foo2 = lambda x: unichr(int(x.group(0)[2:7]))
>>> print re.sub(r'&#\d+;', foo2, c)
艾莉西亚 凯斯(Alicia Keys)
- 谷歌音乐搜索

你可能感兴趣的:(re.sub)