Skip to main content

Normalizing Math Delimiters with Regular Expressions

·63 words·1 min
Author
Che

Regular expressions for normalizing common Markdown math delimiters.

Canonical Form
#

TypeDelimiter
Inline$...$
Display$$...$$

Inline Math
#

InputRegular expression
\(...\)(?<!\\)\\\((.*?)\\\)
\\(...\\)(?<!\\)\\\\\((.*?)\\\\\)
$...$(?<!\$)\$(?!\$)(.*?)(?<!\$)\$(?!\$)

Display Math
#

InputRegular expression
\[...\](?s)(?<!\\)\\\[\s*(.*?)\s*\\\]
\\[...\\](?s)(?<!\\)\\\\\[\s*(.*?)\s*\\\\\]
$$...$$(?s)(?<!\$)\$\$(?!\$)\s*(.*?)\s*(?<!\$)\$\$(?!\$)
equation environment(?s)\\begin\{equation\*?\}\s*(.*?)\s*\\end\{equation\*?\}

Each expression captures the formula body in group 1. Depending on the tool, reference it as $1 or \1.