In [1]:
import re
In [3]:
# re.findall
# re.search vs re.match
# re.sub
In [5]:
s = 'the dog smells'
In [13]:
if re.match('.*dog.*$', s):
    print 'found it!'
else:
    print 'not here'
found it!

In [11]:
if re.search('dog', s):
    print 'found it!'
else:
    print 'not here'
found it!

In [14]:
re.search('dog', s)
Out[14]:
<_sre.SRE_Match at 0x10682b1d0>
In [30]:
match_object = re.search('dog', s)
In [31]:
match_object.start(), match_object.end(), match_object.span()
Out[31]:
(4, 7, (4, 7))
In [32]:
s
Out[32]:
'the dog smells'
In [33]:
bracket = 3
s[match_object.start()-bracket : match_object.end()+bracket]
Out[33]:
'he dog sm'
In [28]:
import nltk
In [29]:
nltk.util.re_show('.* dog', s)
{the dog} smells

In [34]:
type(match_object)
Out[34]:
_sre.SRE_Match
In [35]:
if match_object:
    print "I'm true"
I'm true

In [36]:
match_object = None
In [47]:
s = 'Business: $800 Personal: $189'
pattern = r'Business: \$(\d+)'
m = re.search(pattern, s)
if m:
    print m.group(1)
else:
    print 'you had no business expenses.'
800

In [57]:
s = 'B $800 8000000000000000000 B P $189 P'
pattern = r'(B|P) \$(\d+) \2'
m = re.search(pattern, s)
if m:
    print m.group(2)
else:
    print 'you had no business expenses.'
800

In [61]:
s = 'Andrew Rosenberg: I always say, "Andrew Hates speaking in the third person"'
pattern = r'((\w+) \w+): .*\1.*'
m = re.search(pattern, s)
if m:
    print m.group(1), 'definitely uses the third person'
else:
    print 'nope'
nope

In [69]:
p = r'([A-Z])([A-Z])([A-Z])\3\2\1'
s = 'ABCCBA'
print re.search(p, s).group()
ABCCBA

In []: