[Tex/LaTex] How to escape properly and output regex in latex

text manipulationverbatim

Escaping regex is a heck of a work on LaTeX. How should I write in latex syntax such that I can output the following regexes:

^\(?\s?\w{1,5}\s*[):.]\s*

and

[\[\(].{1,}[\]\)]

I've tried \text{[\[\(].{1,}[\]\)]} but it gives compilation errors.

Best Answer

You can use a verbatim (fancyvrb package) or listings environnement.

\documentclass{article}
\usepackage{fancyvrb}
\begin{document}

\begin{Verbatim}[frame=single]
    ^(?\s?\w{1,5}\s*[):.]\s*
\end{Verbatim}

\end{document}

enter image description here

Related Question