匹配多个字符串

 1 import re 
 2 pattern = 'aa|bb|cc'
 3 s = 'aa'
 4 o = re.match(pattern,s)
 5 print(o)
 6 print(o.group())
 7 
 8 s = 'bb'
 9 w = re.match(pattern,s)
10 print(w)
11 print(w.group())
12 
13 s = 'my name is cc'
14 f = re.search(pattern,s)
15 print(f)
16 print(f.group())
17 
18 print('匹配0-100之间所有的数字')
19 pattern = r'[1-9]?\d$|100$'
20 s = '1'
21 # <re.Match object; span=(0, 1), match='1'>
22 s = '11'
23 # <re.Match object; span=(0, 2), match='11'>
24 s = '100'
25 # <re.Match object; span=(0, 3), match='100'>
26 s = '1000'
27 # None
28 m = re.match(pattern,s)
29 print(m)
1 <re.Match object; span=(0, 2), match='aa'>
2 aa
3 <re.Match object; span=(0, 2), match='bb'>
4 bb
5 <re.Match object; span=(11, 13), match='cc'>
6 cc
7 匹配0-100之间所有的数字
8 None

 

posted @ 2020-05-17 17:59  小他_W  阅读(774)  评论(0编辑  收藏  举报