Regular Expressions

Links

Regular expression operations

Example

#! /usr/bin/python3
# ==================================================================
# test using RegExp in Python3 to modify a file. In this case it
# is a HTML file.
# ------------------------------------------------------------------
#
# Notes:
#
# 1. This code assumes my coding style. For example
#    <title>........</title> is on a single line.
#    Other single lines include:
#    <meta name="author" ....................... />
#    <link rel="stylesheet"..................... />
#    <link rel="icon" .......................... />
#    <link rel="shortcut icon" ................. />
#
# 2. A special case of the single line rule is
#    surounded by footer tags:
#    <footer>
#      <modate>Last Modified: ....... </modate>
#    </footer>
#
# ==================================================================

import re

pattern1     = r'^<title>(.*)</title>$'
replacement1 = '<title>Lots of My Stuff</title>'

pattern2     = r'^<link rel="icon"(.*)/>'
replacement2 = \
    '<link rel="icon"          href="xyz.png" type="image/png" />'

pattern3     = r'^<link rel="shortcut icon"(.*)/>'
replacement3 = \
    '<link rel="shortcut icon" href="xyz.png" type="image/png" />'


file = "index.html"

# ---- open the input file

inFile = open(file,'r')

if not inFile:
    print("File open failed ({})".format(file))
    quit()

# ---- process every line in the file

for line in inFile:

    # ---- strip leading and trailing whitespace

    line = line.strip()

    # --- output the line replacing the title line, etc.

    if re.search(pattern1,line):
        print(replacement1)
    elif re.search(pattern2,line):
        print(replacement2)
    elif re.search(pattern3,line):
        print(replacement3)

    else:
        print(line)

# ---- close the input file

inFile.close()