Python: Having confusion in Regex -
my code (in python 2.7 using anaconda),
import re regex = r"([a-z]+) (\d+)" str = "hello 3 hello regex example" matchobject = re.search(regex, str) print matchobject if matchobject not none: print matchobject.start() print matchobject.end() print matchobject.group(0) print matchobject.group(1) print matchobject.group(2)
when regex search pattern return output in format:
line 1: <_sre.sre_match object @ 0x10a2568b0> line 2: 0 line 3: 7 line 4: hello 3 line 5: hello line 6: 3
i have added line number in output better understanding, line 1 of output confusing, , line 3 (output=7) confusing. can explain line 1 , line 3 of output?
those print numbers correspond print statements
print matchobject #line 1, python has interpret regex match object string , gives it's type , address if matchobject not none: print matchobject.start() #line 2, full match "hello 3", 0 chars away start of input print matchobject.end() #line 3, full match "hello 3", ends after 7 characters print matchobject.group(0) #line 4, full match (regex group 0) "hello 3" print matchobject.group(1) #line 5, captured hello first parens print matchobject.group(2) #line 6, captured 3 second
nothing here seems incorrect me
Comments
Post a Comment