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]+' # 方法三 \s匹配空白
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
"""
posted @ 2019-08-16 17:25  Ranxf  阅读(733)  评论(0编辑  收藏  举报