Reputation: 57
My code is (in Python 2.7 using 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)
When Regex search for pattern it return an output in this format:
line 1: <_sre.SRE_Match object at 0x10a2568b0>
line 2: 0
line 3: 7
line 4: HELLO 3
line 5: HELLO
line 6: 3
I have added line number in output for better understanding, line 1 of output is very confusing, and line 3 (output=7) is also confusing. Can you explain line 1 and line 3 of output?
Upvotes: 0
Views: 61
Reputation: 940
Those print numbers correspond to the print statements
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
nothing here seems incorrect to me
Upvotes: 2