>>> import re
>>> re.search('[abc]','mark')
<_sre.SRE_Match object; span=(1, 2), match='a'>
>>> re.sub('[abc]','o','Mark')
'Mork'
>>> re.sub('[abc]','o', 'carp')
'oorp'
import re
def plural(noun):
if re.search('[sxz]$',noun):
return re.sub('$','es',noun)
elif re.search('[^aeioudgkprt]h$',noun):
return re.sub('$','es',noun)
elif re.search('[^aeiou]y$',noun):
return re.sub('y$','ies',noun)
else:
return noun+'s'
注:
[abc] --匹配a,b,c中的任何一个字符
[^abc] -- 匹配除了a,b,c的任何字符
>>> import re
>>> re.search('[^aeiou]y$','vancancy')
<_sre.SRE_Match object; span=(6, 8), match='cy'>
>>> re.sub('y$','ies','vacancy')
'vacancies'
>>> re.sub('([^aeiou])y$',r'\1ies','vacancy')
'vacancies'
注:
\1-- 表示将第一个匹配到的分组放入该位置。 如果有超过一个分组,则可用\2, \3等等。
函数列表:
a. 在动态函数中使用外部参数值的技术称为闭合。
import re
def build_match_and_apply_functions(pattern,search,replace):
def matches_rule(word):
return re.search(pattern,word)
def apply_rule(word):
return re.sub(search,replace,word)
return (matches_rule,apply_rule)
b.
>>> patterns = (
('[sxz]$','$', 'es'),
('[aeioudgkprt]h$', '$', 'es'),
('(qu|[^aeiou])y$', 'y$','ies'),
('$', '$', 's')
)
>>> rules = [build_match_and_apply_functions(pattern, search, replace) for (pattern, search, replace) in patterns]
>>> rules
[(<function build_match_and_apply_functions.<locals>.matches_rule at 0x10384d510>, <function build_match_and_apply_functions.<locals>.apply_rule at 0x10384d598>), (<function build_match_and_apply_functions.<locals>.matches_rule at 0x10384d620>, <function build_match_and_apply_functions.<locals>.apply_rule at 0x10384d6a8>), (<function build_match_and_apply_functions.<locals>.matches_rule at 0x10384d730>, <function build_match_and_apply_functions.<locals>.apply_rule at 0x10384d7b8>), (<function build_match_and_apply_functions.<locals>.matches_rule at 0x10384d840>, <function build_match_and_apply_functions.<locals>.apply_rule at 0x10384d8c8>)]
>>>
c.匹配模式文件
import re
def build_match_and_apply_functions(pattern, search,replace):
def matches_rule(word):
return re.search(pattern,word)
def apply_rule(word):
return re.sub(search,replace,word)
return (matches_rule,apply_rule)
rules = []
with open('plural4-rules.txt', encoding = 'utf-8') as pattern_file:
for line in pattern_file:
pattern,search,replace = line.split(None,3)
rules.append(build_match_and_apply_functions(pattern,search,replace))
1) open():打开文件并返回一个文件对象。
2) 'with':创建了一个context:当with语句结束时,python自动关闭文件,即便是打开是发生意外。
3)split(None,3): None表示对任何空白字符(空格,制表等)进行分隔。3表示针对空白分隔三次,丢弃该行剩下的部分。
生成器:
>>> def make_counter(x):
print('entering make_counter')
while True:
yield x
print('incrementing x')
x=x+1
>>> counter = make_counter(2)
>>> counter
<generator object make_counter at 0x1038643f0>
>>> next(counter)
entering make_counter
2
>>> next(counter)
incrementing x
3
>>> next(counter)
incrementing x
4
>>> next(counter)
incrementing x
5
def fib(max):
a,b = 0, 1
while a < max :
yield a
a, b = b, a+b
>>> for n in fib(100):
print(n,end=' ')
0 1 1 2 3 5 8 13 21 34 55 89
>>> list(fib(200))
[0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144
1)将一个生成器传递给list()函数,它将遍历整个生成器并返回所有生成的数值。