Code: Alles auswählen
import re
p = re.compile('alt=(?:"|\'|)[a-zA-Z0-9]*(?:"|\'|)')
m = p.search('<img src="test.gif" alt="myAltTxt" />')
if m:
print 'Match found: ', m.group()
else:
print 'No match'
Code: Alles auswählen
import re
p = re.compile('alt=(?:"|\'|)[a-zA-Z0-9]*(?:"|\'|)')
m = p.search('<img src="test.gif" alt="myAltTxt" />')
if m:
print 'Match found: ', m.group()
else:
print 'No match'
Code: Alles auswählen
import re
def main():
template = '<img src="test.gif" alt=%s width="42" />'
tests = ('"O\'Reilly"', "'my alt txt'", 'myAltTxt')
alt_re = re.compile(r'alt=(((?P<delim>["\'])(.*?)(?P=delim))|(\S+))')
for test in tests:
match = alt_re.search(template % test)
if match:
if match.group('delim') is not None:
alt_value = match.group(4)
else:
alt_value = match.group(5)
print 'Match found:', alt_value
else:
print 'No match'