17. Регулярные выражения.

import re

pattern = "abc"
match = re.match(pattern, "abc")
print match.pos
0
print re.match("^abc$", "abc")
print re.match("^abc$", "abcd")
<_sre.SRE_Match object at 0x7fb61006b9f0>
None
In [46]:
print re.match("abc", "dabc")
print re.search("abc", "dabc")

None
<_sre.SRE_Match object at 0x7fb61004f578>

In [44]:
help(re.match)

Help on function match in module re:

match(pattern, string, flags=0)
    Try to apply the pattern at the start of the string, returning
    a match object, or None if no match was found.

In [43]:
help(re.search)

Help on function search in module re:

search(pattern, string, flags=0)
    Scan through string looking for a match to the pattern, returning
    a match object, or None if no match was found.

In [47]:
print re.match("a[0-9]b", "a1b")

<_sre.SRE_Match object at 0x7fb61004f510>

In [48]:
print re.match("a[0-9]*b", "a1124234234b")

<_sre.SRE_Match object at 0x7fb61004f4a8>

In [49]:
print re.match("a[0-9]*b", "ab")
print re.match("a[0-9]+b", "ab")

<_sre.SRE_Match object at 0x7fb61004f5e0>

None
In [51]:
match = re.match("(.+)@(.+\.com)", "user@domain.com")
print match.group()

('user', 'domain.com')