pyhtonの正規表現チェッカー+コード

www.pyregex.com

 

意外と便利だったので。

import re

def findtags1(text):
params = '(\w+\s*=\s*"[^"]*"\s*)*'
tags = '(<\s*\w+\s*' + params + '\s*/?>)'
return re.findall(tags, text)


def findtags2(text):
m = re.findall(r'<[^\/<>]*[ab].*?>', text)
return m

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 test1():
assert findtags1(testtext1) == ['<a href="www.udacity.com">',
'<b>',
'<a href="www.udacity.com"target="_blank">']
assert findtags1(testtext2) == []
assert findtags1(testtext3) == ['< b >']
return 'tests pass'

def test2():
assert findtags2(testtext1) == ['<a href="www.udacity.com">',
'<b>',
'<a href="www.udacity.com"target="_blank">']
assert findtags2(testtext2) == []
assert findtags2(testtext3) == ['< b >']
return 'tests pass'

print(test1())
print(test2())