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

# 匹配由单个空格分隔的任意单词对,也就是姓和名
 import re

 patt = '[A-Za-z]+ [A-Za-z]+'  # 方法一 +加号操作符匹配它左边的正则表达式至少出现一次的情况
# patt = '[A-Za-z]+[ A-Za-z]+' # 方法二
5 # patt = '[A-Za-z]+\s[A-Za-z]+' # 方法三 \s匹配空白
name1 = 'zhang san'
name2 = 'li si'
name3 = 'wang ermazi' m = re.match(patt, name1)
m1 = re.match(patt, name2)
m2 = re.match(patt, name3)
if m is not None:
print(m.group())
if m is not None:
print(m1.group())
if m is not None:
print(m2.group())
"""
/usr/bin/python3.5 /studydisk/other/正则表达式/demo2.py
zhang san
li si
wang ermazi
"""
上一篇:js实时显示鼠标坐标


下一篇:C语言将字符串转json