forked from adaptives/python-examples
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpython_regex.py
More file actions
executable file
·26 lines (22 loc) · 929 Bytes
/
python_regex.py
File metadata and controls
executable file
·26 lines (22 loc) · 929 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#!/usr/bin/python
# Filename : python_regex.py
import re
if None == re.match("c", "abcdef"): # No match
print "Odd but true: didn't match \"c\" in \"abcdef\""
else:
print "matched c in abcdef"
if re.search("c", "abcdef"):
print "As expected: found \"c\" in search of \"abcdef\""
else:
print "did not find c in search of abcdef"
if re.search("g", "abcdef"):
print "found g in search of abcdef"
else:
print "As expected: did not find \"g\" in search of \"abcdef\""
# Word Boundaries are not \< and \>
# Thou shalt useth raw strings to saveth thineself from backslash hell (i.e \\\\b)
re_abcs = re.compile(r'\babc\b') # this would be /\<abc\>/ in other languages
if re_abcs.search("I know my abc's"):
print "Not PERL alert: As found \"r'\\babc\\b'\" (i.e. \"/\\<abc\\>/\") in compiled search of \"I know my abc's\""
else:
print "did not find '\<abc\>' in compiled search of \"I know my abc's\""