info = '''<table>
    <tr align = "center">
        <h1> Lachlan Osborn </h1>
        <p> Address: 5 Smith Street, Manly <br>
        Date of Birth: 26th April 1993 </p>
        
        <a href="semester.html"><b>My Semester Units</b></a>
        <p><b>Check out my <a href="hobbies.html">hobbies.</a></b></p>
    </tr>
</center>'''

def remove_html(text, info):
    import re
    text = re.sub(r'<.*?>', '', info)
    return text

remove_html(text.strip())

Dani AI

Generated

Quick practical note for : ’s regex is a handy quick fix for very simple, already-sanitized strings, but it’s brittle for real HTML. Regexes break on nested tags, comments, script/style blocks, attributes that contain “>”, and on input that’s entity-escaped. For reliable results, decode entities first and use an HTML parser to extract text.

A robust, easy approach is BeautifulSoup (works well with messy markup and gives control over what to remove). For example:

from bs4 import BeautifulSoup
import html

def html_to_text(s):
    s = html.unescape(s)                 # decode entities like &lt; &gt;
    soup = BeautifulSoup(s, "html.parser")
    for tag in soup(["script", "style"]):
        tag.decompose()                  # drop scripts/styles
    return soup.get_text(separator="\n", strip=True)

If you cannot install third-party packages, the stdlib has a lighter fallback using html.parser. It is less forgiving but avoids external deps:

from html.parser import HTMLParser
import html

class TextExtractor(HTMLParser):
    def __init__(self):
        super().__init__()
        self.parts = []
    def handle_data(self, d):
        self.parts.append(d)
    def get_text(self):
        return ''.join(self.parts)

def html_to_text_stdlib(s):
    s = html.unescape(s)
    p = TextExtractor()
    p.feed(s)
    return p.get_text().strip()

Tips: remove script/style elements before extracting, use separator in get_text() to preserve line breaks, collapse excessive whitespace with ' '.join(text.split()) if needed, and prefer parser-based methods when input is untrusted or complex. For trivial, strictly controlled input a simple regex can suffice, but prefer parsers for correctness and maintainability.

You have a post with same question.
http://www.daniweb.com/software-development/python/threads/416862

In this post you give more info about html file.
What you post now is just a mess,read about function.
Is this a school task? can you use regex in this task?

import re

info = '''<table>
    <tr align = "center">
        <h1> Lachlan Osborn </h1>
        <p> Address: 5 Smith Street, Manly <br>
        Date of Birth: 26th April 1993 </p>

        <a href="semester.html"><b>My Semester Units</b></a>
        <p><b>Check out my <a href="hobbies.html">hobbies.</a></b></p>
    </tr>
</center>'''

def remove_html(info):
    text = re.sub(r'<.*?>', '', info)
    text = text.strip()
    text = text.replace('\n\n', '\n')
    for line in text.split('\n'):
        print line.strip()

remove_html(info)
"""Output-->
Lachlan Osborn
Address: 5 Smith Street, Manly
Date of Birth: 26th April 1993
My Semester Units
Check out my hobbies
"""
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.