This tested snippet should do it:

import re
line = re.sub(r"</?\[\d+>", "", line)

Edit: Here's a commented version explaining how it works:

line = re.sub(r"""
  (?x) # Use free-spacing mode.
  <    # Match a literal '<'
  /?   # Optionally match a '/'
  \[   # Match a literal '['
  \d+  # Match one or more digits
  >    # Match a literal '>'
  """, "", line)

Regexes are fun! But I would strongly recommend spending an hour or two studying the basics. For starters, you need to learn which characters are special: "metacharacters" which need to be escaped (i.e. with a backslash placed in front - and the rules are different inside and outside character classes.) There is an excellent online tutorial at: www.regular-expressions.info. The time you spend there will pay for itself many times over. Happy regexing!

Answer from ridgerunner on Stack Overflow
๐ŸŒ
Note.nkmk.me
note.nkmk.me โ€บ home โ€บ python
Replace Strings in Python: replace(), translate(), and Regex
May 4, 2025 - Use the | operator to match multiple patterns. Each pattern may include special regex characters or literal substrings. This allows you to replace different substrings with the same string.
Discussions

regex - Python string.replace regular expression - Stack Overflow
I have a parameter file of the form: parameter-name parameter-value Where the parameters may be in any order but there is only one parameter per line. I want to replace one parameter's parameter-... More on stackoverflow.com
๐ŸŒ stackoverflow.com
Replace (complicated) RegEx with readable expressions in Python
If you relly want to make it easier to use make it strongly typed as right now it just Look like verbose version of regex with the same drawbacks and more oputrtunity to misspell something or dont know the keyword More on reddit.com
๐ŸŒ r/pythontips
1
4
July 12, 2023
Regex to find and replace with same string plus a suffix
You can do it in sed like this: sed -i.bak "s/^(void .*typeA)/\1_suffix/g" temp More on reddit.com
๐ŸŒ r/learnpython
2
3
October 17, 2022
Regex.Replace headache? How to replace \n in a text with regular expressions?
I recommend using one of those websites where you can enter regex to seeย  how it works instead of trying to write code. You need to look into which characters in the pattern needs to be escaped, like the forward slash. Not sure if reddit markdown removed backslashes or not, Try something like \/\/.*$ ``` More on reddit.com
๐ŸŒ r/csharp
10
0
January 8, 2025
๐ŸŒ
Python documentation
docs.python.org โ€บ 3 โ€บ library โ€บ re.html
re โ€” Regular expression operations
Source code: Lib/re/ This module provides regular expression matching operations similar to those found in Perl. Both patterns and strings to be searched can be Unicode strings ( str) as well as 8-...
๐ŸŒ
W3Schools
w3schools.com โ€บ python โ€บ python_regex.asp
Python RegEx
RegEx can be used to check if a string contains the specified search pattern. ... Python has a built-in package called re, which can be used to work with Regular Expressions.
๐ŸŒ
Pandas
pandas.pydata.org โ€บ docs โ€บ reference โ€บ api โ€บ pandas.Series.str.replace.html
pandas.Series.str.replace โ€” pandas 3.0.5 documentation
Method to replace occurrences of a substring with another substring. ... Extract substrings using a regular expression. ... Find all occurrences of a pattern or regex in each string.
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ python โ€บ python-substituting-patterns-in-text-using-regex
Python - Substituting patterns in text using regex - GeeksforGeeks
July 12, 2025 - # Python implementation of substituting a # specific text pattern in a string using regex # importing regex module import re # Function to perform # operations on the strings def substitutor(): # a string variable sentence1 = "It is raining outside." # replacing text 'raining' in the string # variable sentence1 with 'sunny' thus # passing first parameter as raining # second as sunny, third as the # variable name in which string is stored # and printing the modified string print(re.sub(r"raining", "sunny", sentence1)) # a string variable sentence2 = "Thank you very very much."
Find elsewhere
๐ŸŒ
PythonTest
pythontest.com โ€บ python โ€บ regex-search-replace
Python regex Search and Replace Examples | PythonTest
I think the most basic form of a search/replace script in python is something like this: import fileinput import re for line in fileinput.input(): line = re.sub('foo','bar', line.rstrip()) print(line) The fileinput module takes care of the stream verses filename input handling. The re (regex, regular expression) module has sub which handles the search/replace.
๐ŸŒ
JanBask Training
janbasktraining.com โ€บ community โ€บ python-python โ€บ python-replace-regex
python .replace() regex | JanBask Training Community
May 7, 2025 - `str.replace()` performs a direct substring match. It does not interpret patterns, special characters, or regex tokens. ... Nothing changes because `d+` is treated as a literal string, not a pattern.
๐ŸŒ
PYnative
pynative.com โ€บ home โ€บ python โ€บ regex โ€บ python regex replace pattern in a string using re.sub()
Python Regex Replace Pattern in a string using re.sub()
July 19, 2021 - import re # replacement function to convert uppercase letter to lowercase def convert_to_lower(match_obj): if match_obj.group() is not None: return match_obj.group().lower() # Original String str = "Emma LOves PINEAPPLE DEssert and COCONUT Ice Cream" # pass replacement function to re.sub() res_str = re.sub(r"[A-Z]", convert_to_lower, str) # String after replacement print(res_str) # Output 'Emma loves pineapple dessert and coconut Ice Cream'Code language: Python (python) Run ยท We saw how to find and replace the single regex pattern in the earlier examples.
๐ŸŒ
Plain English
python.plainenglish.io โ€บ the-incredible-power-of-pythons-replace-regex-6cc217643f37
The incredible power of Pythonโ€™s replace regex | by Josh Weinstein | Python in Plain English
November 1, 2020 - The replacement string can be a literal replacement, such as matching [0-9]+ and replacing it with "abc" . Such an operation would convert all digit sequences in a string with the character sequence "abc" .
๐ŸŒ
Python documentation
docs.python.org โ€บ 3 โ€บ howto โ€บ regex.html
Regular expression HOWTO โ€” Python 3.14.7 documentation
If the pattern isnโ€™t found, string is returned unchanged. The optional argument count is the maximum number of pattern occurrences to be replaced; count must be a non-negative integer.
๐ŸŒ
W3Schools
w3schools.com โ€บ python โ€บ ref_string_replace.asp
Python String replace() Method
Remove List Duplicates Reverse ... Study Plan Python Interview Q&A Python Training ... The replace() method replaces a specified phrase with another specified phrase....
๐ŸŒ
TechGeekBuzz
techgeekbuzz.com โ€บ blog โ€บ python-regex-replace-pattern-in-a-string-using-re-sub
Python Regex Replace Pattern in a string using re.sub()
To do this, we learned two regex methods sub() and subn() . The sub() method accept a regular expression pattern, and replace all the matched pattern of the string with the replacement string or function, and return the newly replaced string.
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ python โ€บ python-regex-replace-captured-groups
Python Regex: Replace Captured Groups - GeeksforGeeks
July 23, 2025 - This article dives into one of the crucial aspects of regex in Python: Regex Groups, and demonstrates how to use `re.sub()` for group replacement with practical examples. A group in regex is a part of a pattern that is enclosed in parentheses. Groups allow us to segment a pattern into sub-patterns, making it easier to apply specific operations on each part. These groups are indexed numerically from 1 for the first group and so on. The entire match is group 0. ... Extract parts of the matched string: If we want to extract or capture a specific part of the text that matches the pattern, groups allow us to do this.
๐ŸŒ
Table Convert
tableconvert.com โ€บ home โ€บ table convert
Table Convert - Free Online Table Converter and Generator
July 28, 2025 - Edit data using our advanced online table editor with professional features. Supports deleting empty rows, removing duplicates, data transposition, sorting, regex find & replace, and real-time preview.
๐ŸŒ
Real Python
realpython.com โ€บ replace-string-python
How to Replace a String in Python โ€“ Real Python
October 22, 2025 - In this tutorial, you'll learn how to remove or replace a string or substring. You'll go from the basic string method .replace() all the way up to a multi-layer regex pattern using the sub() function from Python's re module.
๐ŸŒ
Index.dev
index.dev โ€บ blog โ€บ regex-advanced-string-replacement-python
Python Regex Replace: How to Replace Strings Using re Module
The re.sub() function is the primary tool for performing string replacements with regex in Python. It allows you to specify a regex pattern to search for, a replacement string, and the target string where the replacement will occur.
๐ŸŒ
RegExr
regexr.com
RegExr: Learn, Build, & Test RegEx
RegExr is an online tool to learn, build, & test Regular Expressions (RegEx / RegExp).
๐ŸŒ
freeCodeCamp
freecodecamp.org โ€บ news โ€บ javascript-string-replace-example-with-regex
JavaScript String.Replace() Example with RegEx
October 20, 2020 - With RegEx, you can match strings at points that match specific characters (for example, JavaScript) or patterns (for example, NumberStringSymbol - 3a&). The .replace method is used on strings in JavaScript to replace parts of string with characters.