python核心编程正则表达式练习题1-2匹配由单个空格分隔的任意单词对,也就是性和名

# 匹配由单个空格分隔的任意单词对,也就是姓和名
 1 import re
 2 
 3 patt = '[A-Za-z]+ [A-Za-z]+'  # 方法一 +加号操作符匹配它左边的正则表达式至少出现一次的情况
 4 # patt = '[A-Za-z]+[ A-Za-z]+'  # 方法二
5 # patt = '[A-Za-z]+\s[A-Za-z]+' # 方法三
6 name1 = 'zhang san' 7 name2 = 'li si' 8 name3 = 'wang ermazi' 9 10 m = re.match(patt, name1) 11 m1 = re.match(patt, name2) 12 m2 = re.match(patt, name3) 13 if m is not None: 14 print(m.group()) 15 if m is not None: 16 print(m1.group()) 17 if m is not None: 18 print(m2.group())

"""
/usr/bin/python3.5 /studydisk/other/正则表达式/demo2.py
zhang san
li si
wang ermazi
"""

猜你喜欢

转载自www.cnblogs.com/ranxf/p/11365243.html
0条评论
添加一条新回复