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

python版本为3.6.1

源代码如下:

import re

patt = '[A-Za-z]+ [A-Za-z]+'

name1 = 'Lu minfei'
name2 = 'Nuo nuo'
name3 = 'Chu zihang'

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())


任意单词为[A-Za-z],匹配一个或多个为’+’,即可写出答案。

猜你喜欢

转载自blog.csdn.net/qq_38115310/article/details/82223204