Welcome to Software Development on Codidact!
Will you help us build our independent community of developers helping developers? We're small and trying to grow. We welcome questions about all aspects of software development, from design to code to QA and more. Got questions? Got answers? Got code you'd like someone to review? Please join us.
Python-compatible regex for Markdown tables
My goal is to programatically convert Markdown tables to their HTML equivalents using regular expressions. I haven't been able to find any Python-compatible regexes for Markdown tables online, so I've been attempting to build my own. However, I've run into a bit of a snag. This is my code so far:
import re
# The text to be converted
markdown = """| Syntax | Description |
| ----------- | ----------- |
| Header | Title |
| Paragraph | Text |
"""
# Handles complex elements of the syntax
def assembleTable(match):
markdown = match.group(0)
# The problem line
html = re.sub(r'^\|(.*)\|\n(?:\| -{3,} )+\|$', r'<tr>\g<1></tr>', markdown, flags=re.MULTILINE)
return f'<table>\n{html}\n</table>'
# Basic table matching
html = re.sub(r'(?:^\|.*\|$\n?)+', assembleTable, markdown, flags=re.MULTILINE)
print(html)
It identifies a Markdown table and passes it into another function for processing, but the next step is where my trouble arises. The regex ^\|(.*)\|\n(?:\| -{3,} )+\|$ successfully matches a line of column headers, but I need to capture the individual headers' text so I can put them in <th></th> tags. I've been experimenting on regex101.com with (\| (.+?) )+ and ( .+? +?\|?), but the headers' variable number and whitespace is throwing me off and making it hard to integrate into the header regex.
How can I capture the individual column headers' text?
2 answers
The following users marked this post as Works for me:
| User | Comment | Date |
|---|---|---|
| Michael | (no comment) | Jan 13, 2026 at 21:43 |
For an arbitrary number of rows and columns, a single regex won't work. You'll need to loop through all cells, replacing them as they are found. Something like this:
import re
markdown = """| Syntax | Description |
| ----------- | ----------- |
| Header | Title |
| Paragraph | Text |
"""
regex_cell = re.compile(r'\|([^|]+)')
cell = 'th'
previous = ' '
html = '<table>\n<tr>'
for match in regex_cell.finditer(markdown):
text = match[1].strip(' ')
if text == '\n':
# new row, unless it's the all-hyphens line or the end of string
if previous[0] != '-' and match.span()[1] != len(markdown):
html += '</tr>\n<tr>'
elif text[0] == '-': # header already rendered, change to td
cell = 'td'
else:
html += f'<{cell}>{text}</{cell}>'
previous = text
html += '</tr>\n</table>'
print(html)
I used [^|]+ to get the contents of a cell:
-
[^|]is a negated character class, which means "anything that's not inside[^and]". In this case, it's any character that's not a| -
+means "one or more occurrences"
Therefore, [^|]+ means "one or more characters that are not |". You were using .*, which means "zero or more characters" (any characters, including |), but I think it's better to restrict the characters to not include | itself, and to have at least one (not sure if it'll have empty cells, but anyway).
Then I remove the spaces using strip, and check if it's a new line (which indicates a new tr), a hyphen (so I ignore the whole line) or a text (which becomes a td or th cell).
The only case where a single substitution will work is when you know the exact number of columns and rows:
html = re.sub(
r'\|([^|]+)\|([^|]+)\|\n\|[- ]+\|[- ]+\|\n\|([^|]+)\|([^|]+)\|\n\|([^|]+)\|([^|]+)\|',
r'''<table>
<tr>
<th>\1</th>
<th>\2</th>
</tr>
<tr>
<td>\3</td>
<td>\4</td>
</tr>
<tr>
<td>\5</td>
<td>\6</td>
</tr>
</table>''',
markdown
)
In this case, each cell is a capturing group, but look how complicated and - IMO - unreadable the regex became.
Without regex
IMO, regex is not the best tool for this job. You could search for specialized libraries that convert Markdown to HTML.
One example is the Python-Markdown lib:
import markdown
table = r"""| Syntax | Description |
| ----------- | ----------- |
| Header | Title |
| Paragraph | Text |
"""
print(markdown.markdown(table, extensions=['tables']))
Not only it's more straighforward, it'll also handle corner cases, such as escaped characters (if the text is Text \| more text, the | is part of the text, not the separator).
Or, if you don't want to use a lib for that, just break the lines and cells using split:
markdown = """| Syntax | Description |
| ----------- | ----------- |
| Header | Title |
| Paragraph | Text |
"""
cell = 'th'
html = '<table>\n<tr>'
lines = markdown.split('\n')
for i, line in enumerate(lines):
# ignore line with hyphens, and change cell type from th to td
if '----' in line:
cell = 'td'
continue
for text in line.split('|'):
text = text.strip()
if text:
html += f'<{cell}>{text}</{cell}>'
if i < len(lines) - 2:
html += '</tr>\n<tr>'
html += '</tr>\n</table>'
print(html)
Which is easier than a regex to read and maintain, IMO.
0 comment threads
You won't be able to do it for arbitrary length tables in a single regex matching the whole row. Capture groups are replaced when repeated. So any \| (?:(cell_text) \|)+ expression[1] will wind up with the capture group \g<1> as the last cell's text.
Instead, you'd have to send, say \|?[ \t]*(.*?)[ \t]s*\| to re.sub to replace each cell in a row with <td>\1</td> and wrap that in <tr></tr>.
-
Irrespective of whitespace ↩︎

0 comment threads