標籤:
>>> 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 redef 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 redef 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 redef 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 xprint(‘incrementing x‘)x=x+1>>> counter = make_counter(2)>>> counter<generator object make_counter at 0x1038643f0>>>> next(counter)entering make_counter2>>> next(counter)incrementing x3>>> next(counter)incrementing x4>>> next(counter)incrementing x5
def fib(max):a,b = 0, 1while a < max :yield aa, 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()函數,它將遍曆整個產生器並返回所有產生的數值。
Python學習筆記5-閉合與產生器