forked from sbu-python-class/python-science
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathregex.post
More file actions
43 lines (28 loc) · 1.19 KB
/
regex.post
File metadata and controls
43 lines (28 loc) · 1.19 KB
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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
One topic we won't have time to dive deeply in is regular expressions.
This is a shorthand syntax for pattern matching in strings. In
python, the "re" module provides support for regular expressions.
Here's an example:
import re
strings = [r"<a>this is my string</a>",
r"<b>this is a different string</b>"]
# this is the pattern that we will match -- it has 3 groups
re_test = r"<(\w*)>(.*)</(\w*)>"
for s in strings:
a = re.search(re_test, s)
if not a == None:
if a.group(1) == a.group(3):
# we found a match
print "string in '{}' tags is: {}".format(a.group(1), a.group(2))
This will find XML-like tags, <tag>this is text in the tag</tag> and
extra the text associated with each tag.
If you remove the "*" after the "\w", it will restrict itself to
single-character tags.
This webpage:
http://txt2re.com/
will help you design a regular expression for whatever type of
operation you want to do.
Games are even devised around finding complex regexs:
http://xkcd.com/1313/
(note the hover text there has a regular expression that supposedly
will correctly match all the winners of all US Presidental elections,
but not the losers) -- anyone what to try it?