<python cookbook> - 1.18 - 一次完成多个替换
这个blog介绍正则,写得不错,而且,一如既往的‘长’。
1. re.escape(string)
THIS,说明函数的用法、实际用途。
# 1. 基本用法
>>> import re >>> strTest = 'abcdefg' >>> re.escape(strTest) 'abcdefg' >>> strTest = 'abcdefg()4' >>> re.escape(strTest) 'abcdefg\(\)\x04'
# 2. figure out this: '|'.join(map(re.escape, adict)) >>> dictTest = {'a':'1', 'b':'2', 'c':'*', 'd':'$'} >>> re.escape(dictTest) Traceback (most recent call last): File "<stdin>", line 1, in <module> File "/usr/lib/python2.7/re.py", line 214, in escape return pattern[:0].join(s) TypeError: unhashable type >>> map(re.escape, dictTest) ['a', 'c', 'b', 'd'] >>> '|'.join(map(re.escape, dictTest)) 'a|c|b|d'
# 注意到上面的格式是用'|'连接的,简单地说,'|'可以连接多个规则pattern
2. re.compile()
THIS - 简单地说明怎么用。
>>> rx = re.compile('|'.join(map(re.escape, dictTest))) >>> rx <_sre.SRE_Pattern object at 0x852fb80>
3. group([group1, ...])
THIS - 简单地说明怎么用。
# 链接中的事例,简单说,一个(),就是一个group >>> m = re.match(r"(w+) (w+)", "Isaac Newton, physicist") >>> m.group(0) # The entire match 'Isaac Newton' >>> m.group(1) # The first parenthesized subgroup. 'Isaac' >>> m.group(2) # The second parenthesized subgroup. 'Newton' >>> m.group(1, 2) # Multiple arguments give us a tuple. ('Isaac', 'Newton')
4. re.sub
THIS - 简单地说明怎么用。
5. 理解所有 : 一次完成多个替换
import re def multiple_replace(text, adict): rx = re.compile('|'.join(map(re.escape, adict))) def one_xlat(match): return adict[match.group(0)] return rx.sub(one_xlat, text)
6. Python - 闭包
1. Look at THIS
2.
# This definitely is closure. def addx(x): def addy(y): return x+y return addy print 'addx(x):', addx(1)(1) # Is this closure?? def addxy(x, y): def addy(y): return x+y return 0+addy(y) print 'addxy(x, y): %d' % (addxy(1, 1))
END