python 正则表达式出错

Python 正则表达式交互式编程点星出错,显示没有“group”

img

说明mo的值是None

改成这样试试


import re

nameRegex = re.compile(r'First Name:(.*) Last Name:(.*)')
mo = nameRegex.search('First Name:A1 Last Name:Sweigrat')
if mo:
    print(mo.group(1))
else:
    print('No match found')

img

匹配失败了,你的正则是 Firstname ,没有空格且小写n,字符串是有空格且大写N


import re


nameRegex = re.compile(r'First Name:(.*) Last Name:(.*)')

# 方式一:用search
mo = nameRegex.search('First Name:A1 Last Name:Sweigrat')
print(mo.group(1,2))

# 方式二: 用findall
mo = nameRegex.findall('First Name:A1 Last Name:Sweigrat')
print(mo[0])