Question: I was trying to do some pattern matching in python. But I am not able to understand why I get a second match when i just match for only one.

```
import re

def Main():
    m = "12312312ranger12312319"
    pattern = re.compile('(\d$)')
    r = pattern.search(m)
    if r:
        print "Matched " + r.group(0) +  " Second " + r.group(1)
    else:
        print "Not Matched"

if __name__ == '__main__':
    Main()

```

This gives me an output like this 

```
Matched 9 Second 9

```

I am thinking r.group(1) should not be there at all. Am i understanding it wrongly ?

Answer: 
group(0) will always returns the whole text that was matched regardless of if it was captured in a group or not. See example:

```
import re

def Main():
    m = "12312312ranger12312319"
    pattern = re.compile('\d(\d$)')
    r = pattern.search(m)
    if r:
        print r.group(0) + ' ' + r.group(1)
    else:
        print "Not Matched"

if __name__ == '__main__':
    Main()

```

Output:

```
19 9

```
Because you're both matching and capturing the last digit which was at the end of a line. So group(0) and group(1) refer to the same. `(\d$)` not only do capturing but also it will do the job of matching. So finally `group(0)` prints the matched characters and `group(1)` prints all the characters which are present inside the captured group index 1.