# Match 物件

``````>>> import re
>>> m = re.search(r'\d{3}', "Kaohsiung 803, Road 12")
>>> m.group()
'803'
>>> m.start()
10
>>> m.end()
13
``````

`search` 類似的是 `match` 函式，`search` 會在整個字串中，找尋第一個符合的子字串，而 `match` 只會在字串開頭看看接下來的字串是否符合，若有符合也是傳回 match 物件，若無傳回 `None`

``````>>> m = re.match(r'\d{3}', "Kaohsiung 803, Road 12")
>>> m == None
True
>>> m = re.match(r'\d{3}', "803, Road 12")
>>> m.group()
'803'
``````

``````>>> m = re.match(r'(\w+) (\w+)', 'Isaac Newton, physicist')
>>> m.group(0)
'Isaac Newton'
>>> m.group(1)
'Isaac'
>>> m.group(2)
'Newton'
>>> m.group(1, 2)
('Isaac', 'Newton')
``````

``````>>> m = re.match(r'(?P<user>^[a-zA-Z]+\d*)@(?P<preCom>[a-z]+?.)com', 'caterpillar@openhome.com')
>>> m.group('user')
'caterpillar'
>>> m.group('preCom')
'openhome.'
>>> m['user']
'caterpillar'
>>> m[1]
'caterpillar'
>>> m.groups()
('caterpillar', 'openhome.')
>>> m.groupdict()
{'user': 'caterpillar', 'preCom': 'openhome.'}
``````

``````>>> m = re.fullmatch(r'(?P<user>^[a-zA-Z]+\d*)@(?P<preCom>[a-z]+?.)com', 'caterpillar@openhome.com is valid')
>>> m == None
True
>>> re.match(r'(?P<user>^[a-zA-Z]+\d*)@(?P<preCom>[a-z]+?.)com', 'caterpillar@openhome.com is valid')
<re.Match object; span=(0, 24), match='caterpillar@openhome.com'>
``````