Python:在Regex中混淆了

时间:2017-08-14 19:20:08

标签: regex python-2.7

我的代码是(在Python 2.7中使用Anaconda),

import re
regex = r"([A-z]+) (\d+)"
str = "HELLO 3 hello this is REGEX example"
matchObject = re.search(regex, str)
print matchObject
if matchObject is not None:
    print matchObject.start()
    print matchObject.end()
    print matchObject.group(0)
    print matchObject.group(1)
    print matchObject.group(2)

当Regex搜索模式时,它会以这种格式返回输出:

line 1: <_sre.SRE_Match object at 0x10a2568b0>
line 2: 0
line 3: 7
line 4: HELLO 3
line 5: HELLO
line 6: 3

我在输出中添加了行号以便更好地理解,输出的第1行非常混乱,第3行(输出= 7)也令人困惑。 你能解释输出的第1行和第3行吗?

1 个答案:

答案 0 :(得分:2)

这些打印编号对应于打印报表

print matchObject #line 1, python has to interpret your regex match object as a string and gives it's type and address
if matchObject is not None:
    print matchObject.start() #line 2, the full match is "HELLO 3", this is zero chars away from the start of the input
    print matchObject.end() #line 3, the full match is "HELLO 3", which ends after 7 characters
    print matchObject.group(0) #line 4, the full match (regex group 0) is "HELLO 3"
    print matchObject.group(1) #line 5, you captured HELLO with the first parens
    print matchObject.group(2) #line 6, you captured 3 with the second

这里似乎没有什么不对我