engl-2311-blog/regex-chicanery.py

14 lines
407 B
Python
Raw Normal View History

2024-08-25 22:52:09 -05:00
#!/usr/bin/env python3
import re
import sys
# add title attribute to img tags
filename = sys.argv[1]
with open(filename, 'r+') as f:
contents = ''.join(f.readlines())
regexp = re.compile('alt="(.*?)"')
for match in regexp.finditer(contents):
contents = contents.replace(match.group(0), f'title="{match.group(1)}" {match.group(0)}')
with open(filename, 'wt') as f:
f.write(contents)