-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfindHtmlTags.py
56 lines (47 loc) · 1.7 KB
/
findHtmlTags.py
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
44
45
46
47
48
49
50
51
52
53
54
55
56
# ---------------
# User Instructions
#
# Write a function, findtags(text), that takes a string of text
# as input and returns a list of all the html start tags in the
# text. It may be helpful to use regular expressions to solve
# this problem.
import re
from string import split
def findtags(text):
tags = []
for line in split(text, '\n'):
matchResult = re.findall('<\s*[a-z]+\s*[\w\"=\.]*>', line)
if matchResult: tags.append(*matchResult)
print tags
return tags
def returnTagFromLine(line):
m = re.match('(<\s*[a-z]+\s*[\w\"=\.]+>)', line)
print re.findall('<[a-z]+ [\w\"=\.]+>', line)
if m: print m.group(1)
#return Fail if (not m) else (m.group(1), text[m.end():])
testtext1 = """
My favorite website in the world is probably
<a href="www.udacity.com">Udacity</a>. If you want
that link to open in a <b>new tab</b> by default, you should
write <a href="www.udacity.com"target="_blank">Udacity</a>
instead!
"""
testtext2 = """
Okay, so you passed the first test case. <let's see> how you
handle this one. Did you know that 2 < 3 should return True?
So should 3 > 2. But 2 > 3 is always False.
"""
testtext3 = """
It's not common, but we can put a LOT of whitespace into
our HTML tags. For example, we can make something bold by
doing < b > this < /b >, Though I
don't know why you would ever want to.
"""
def test():
assert findtags(testtext1) == ['<a href="www.udacity.com">',
'<b>',
'<a href="www.udacity.com"target="_blank">']
assert findtags(testtext2) == []
assert findtags(testtext3) == ['< b >']
return 'tests pass'
print test()