2

I have some difficulties with formatting regular expressions in the argument of \verb. Thank you for any help.

\documentclass[11pt]{book}
\usepackage[paperwidth=16cm, paperheight=24cm]{geometry}
\usepackage[T1]{fontenc}
\usepackage[french]{babel}
\usepackage{caption}
\usepackage{mathtools}
\usepackage{makecell}
\usepackage{tabularx}
\usepackage{ragged2e}

\newcolumntype{L}[1]{>{\RaggedRight}X} % new

\input{structure.cls} %

\begin{document}
    \begin{table}[!htp]                 % new
    \small\linespread{0.84}\selectfont  % reduce interline spaces
    \setlength\tabcolsep{4pt}           % new
    \setcellgapes{2pt}                  % new
    \makegapedcells
        \begin{tabularx}{\linewidth}{|l|L{4cm}|l|} % new
            \hline
                \bfseries{Pour trouver} & \bfseries\makecell{RegEx à utiliser} & \bfseries{Exemple} \\
            \hline
                E-mail & \verb+[^[\w\.=-]+@[\w\.-]+\.[\w]{2,3}\$]+ & [email protected] \\
            \hline
                Numéros de sécurité sociale américains & \verb+[^[a-z]+\$]+ & 513-84-7329 \\
            \hline
                Adresses IPV4 & \verb+[^[\w\.=-]+@[\w\.-]+\.[\w]{2,3}\$]+ & 212.23.2.110\\
            \hline
                Dates au format MM/JJ/AAAA & \verb+[^\d{1,3}[.]\d{1,3}[.]\d{1,3}[.]\d{1,3}\$]+ & Un seul mot non vide en minuscule \\
            \hline
                Numéros de cartes Mastercard & \verb+[^(?:5[1-5][0-9]{2}|222[1-9]|22[3-9][0-9]|2[3-6][0-9]{2}|27[01][0-9]|2720)[0-9]{12}\$]+ & Exemple \\
            \hline
                Numéros de cartes Visa & \verb+[\b([4]\d{3}[\s]\d{4}[\s]\d{4}[\s]\d{4}|[4]\d{3}[-]\d{4}[-]\d{4}[-]\d{4}|[4]\d{3}[.]\d{4}[.]\d{4}[.]\d{4}|[4]\d{3}\d{4}\d{4}\d{4})\b]+ & Exemple \\
            \hline
                Numéros de cartes American Express & \verb+[^3[47][0-9]{13}\$]+ & Exemple \\
            \hline
                Codes ZIP US & \verb+[^((\d{5}-\d{4})|(\d{5})|([A-Z]\d[A-Z]\s\d[A-Z]\d))\$]+ & Exemple \\
            \hline
                Chemins d’accès à des fichiers & \verb+[\\[^\\]+\$]+\$]+ & Exemple \\
            \hline
                URL & \verb+[(?i)\b((?:[a-z][\w-]+:(?:\/{1,3}|[a-z0-9\%])|www\d{0,3}[.]|[a-z0-9.\-]+[.][a-z]{2,4}\/)(?:[^\s()<>]+|\(([^\s()<>]+|(\([^\s()<>]+\)))*\))+(?:\(([^\s()<>]+|(\([^\s()<>]+\)))*\)|[^\s`!()\[\]{};:'".,<>?<<>>""'']))]+ & Exemple
            \hline
        \end{tabularx}
        \caption{Exemples d'expressions régulières couramment utilisées}\label{tab:regexCourammentUtilisees}
    \end{table}
\end{document}

Header of Latex file

\documentclass[12pt]{book}
\usepackage[paperwidth=20cm, paperheight=29cm, margin=2cm]{geometry}
\usepackage[T1]{fontenc}
\usepackage[french]{babel}
\usepackage{pifont} %Sutilise avec \usepackage{amsmath} pour la commande $\text{\ding{113}}  \Delta$
\usepackage{amsmath}
\usepackage{adjustbox}
\usepackage[activate={true,nocompatibility},final,tracking=true,kerning=true,spacing=true,factor=1100,stretch=10,shrink=10]{microtype}
\usepackage{multicol}
\usepackage{mathtools}
\usepackage{makecell}
\usepackage{tabularx}
\usepackage{listings}
\usepackage{ragged2e}

\usepackage[shortlabels]{enumitem} % Pour \begin{enumerate}a)
\usepackage{xcolor}
\usepackage{hyperref}
\usepackage{caption}
\usepackage{paracol} %Permet de diviser la page en 2 colonnes
\usepackage{titlesec} %Pour modifier le fomat du titre de section subparagraph
\usepackage{cancel} %Pour écrire chiffres barrés

\newcolumntype{L}[1]{>{\RaggedRight}X} % new
\newcolumntype{P}[1]{>{\RaggedRight}p{#1}}

\usepackage{pdflscape} % for 'landscape' env.
\usepackage{booktabs}  % for well-spaced horizontal rules
\usepackage{xurl}      % for '\path' macro, allow line breaks at arbitrary points

\titleformat*{\subparagraph}{\bfseries\itshape} %Modification du fomat du titre de section subparagraph

\definecolor{light-gray}{gray}{0.9}
\newcommand{\code}[1]{\colorbox{light-gray}{\texttt{#1}}}
\DeclareMathAlphabet\mathsf{OT1}{lcmss}{m}{n}
1
  • {tabularx} does not allow verbatim elements. You should try {NiceTabularxX} of nicematrix which allows those elements (but you need several compilations). Oct 20, 2023 at 17:08

3 Answers 3

2

You can't use + inside the verbatim text delimited by `\verb+...+. As a delimiter you must use a character that doesn't appear in the verbatim text.

I suggest " that's unlikely to appear in regexes, but because of Murphy's law it does appear in the last one! (I used T.)

Also, you need to break lines. I'd make three equal size L columns (by the way, your definition requires an argument that's not used, so I removed it) and use fvextra features, see the code.

Of course, I'd also recommend to remove the vertical rules, but see below for a more appealing version.

\documentclass[11pt]{book}
\usepackage[paperwidth=16cm, paperheight=24cm]{geometry}
\usepackage[T1]{fontenc}
\usepackage[french]{babel}
\usepackage{caption}
\usepackage{mathtools}
\usepackage{makecell}
\usepackage{tabularx}
\usepackage{ragged2e}
\usepackage{fvextra}

\newcolumntype{L}{>{\RaggedRight}X} % new

\begin{document}

\begin{table}[!htp] % new

\linespread{0.84}
\footnotesize
\setlength\tabcolsep{4pt}   % new
\setcellgapes{2pt}  % new
\makegapedcells
\fvset{breaklines,breakanywhere,breakanywheresymbolpre={}}

\begin{tabularx}{\linewidth}{|L|L|L|} % new
\hline
\bfseries Pour trouver & \bfseries RegEx à utiliser & \bfseries Exemple \\
\hline
E-mail & \Verb"[^[\w\.=-]+@[\w\.-]+\.[\w]{2,3}\$]" & [email protected] \\
\hline
Numéros de sécurité sociale américains & \Verb"[^[a-z]+\$]" & 513-84-7329 \\
\hline
Adresses IPV4 & \Verb"[^[\w\.=-]+@[\w\.-]+\.[\w]{2,3}\$]" & 212.23.2.110\\
\hline
Dates au format MM/JJ/AAAA & \Verb"[^\d{1,3}[.]\d{1,3}[.]\d{1,3}[.]\d{1,3}\$]" & Un seul mot non vide en minuscule \\
\hline
Numéros de cartes Mastercard & \Verb"[^(?:5[1-5][0-9]{2}|222[1-9]|22[3-9][0-9]|2[3-6][0-9]{2}|27[01][0-9]|2720)[0-9]{12}\$]" & Exemple \\
\hline
Numéros de cartes Visa & \Verb"[\b([4]\d{3}[\s]\d{4}[\s]\d{4}[\s]\d{4}|[4]\d{3}[-]\d{4}[-]\d{4}[-]\d{4}|[4]\d{3}[.]\d{4}[.]\d{4}[.]\d{4}|[4]\d{3}\d{4}\d{4}\d{4})\b]" & Exemple \\
\hline
Numéros de cartes American Express & \Verb"[^3[47][0-9]{13}\$]" & Exemple \\
\hline
Codes ZIP US & \Verb"[^((\d{5}-\d{4})|(\d{5})|([A-Z]\d[A-Z]\s\d[A-Z]\d))\$]" & Exemple \\
\hline
Chemins d’accès à des fichiers & \Verb"[\\[^\\]+\$]+\$]" & Exemple \\
\hline
URL & \Verb T[(?i)\b((?:[a-z][\w-]+:(?:\/{1,3}|[a-z0-9\%])|www\d{0,3}[.]|[a-z0-9.\-]+[.][a-z]{2,4}\/)(?:[^\s()<>]+|\(([^\s()<>]+|(\([^\s()<>]+\)))*\))+(?:\(([^\s()<>]+|(\([^\s()<>]+\)))*\)|[^\s`!()\[\]{};:'".,<>?<<>>""'']))]T & Exemple \\
\hline
\end{tabularx}

\caption{Exemples d'expressions régulières couramment utilisées}\label{tab:regexCourammentUtilisees}

\end{table}


\begin{table}[!htp] % new

\footnotesize\raggedright
\fvset{breaklines,breakanywhere,breakanywheresymbolpre={}}

\textbf{E-mail} \\
\Verb"[^[\w\.=-]+@[\w\.-]+\.[\w]{2,3}\$]" \\
\texttt{[email protected]}

\medskip

\textbf{Numéros de sécurité sociale américains} \\
\Verb"[^[a-z]+\$]" \\
513-84-7329

\medskip

\textbf{Adresses IPV4} \\
\Verb"[^[\w\.=-]+@[\w\.-]+\.[\w]{2,3}\$]" \\
212.23.2.110

\medskip

\textbf{Dates au format MM/JJ/AAAA} \\
\Verb"[^\d{1,3}[.]\d{1,3}[.]\d{1,3}[.]\d{1,3}\$]" \\
Exemple

\medskip

\textbf{Numéros de cartes Mastercard} \\
\Verb"[^(?:5[1-5][0-9]{2}|222[1-9]|22[3-9][0-9]|2[3-6][0-9]{2}|27[01][0-9]|2720)[0-9]{12}\$]" \\
Exemple

\medskip

\textbf{Numéros de cartes Visa} \\
\Verb"[\b([4]\d{3}[\s]\d{4}[\s]\d{4}[\s]\d{4}|[4]\d{3}[-]\d{4}[-]\d{4}[-]\d{4}|[4]\d{3}[.]\d{4}[.]\d{4}[.]\d{4}|[4]\d{3}\d{4}\d{4}\d{4})\b]" \\
Exemple

\medskip

\textbf{Numéros de cartes American Express} \\
\Verb"[^3[47][0-9]{13}\$]" \\
Exemple

\medskip

\textbf{Codes ZIP US} \\
\Verb"[^((\d{5}-\d{4})|(\d{5})|([A-Z]\d[A-Z]\s\d[A-Z]\d))\$]" \\
Exemple

\medskip

\textbf{Chemins d’accès à des fichiers} \\
\Verb"[\\[^\\]+\$]+\$]" \\
Exemple

\medskip

\textbf{URL} \\
\Verb T[(?i)\b((?:[a-z][\w-]+:(?:\/{1,3}|[a-z0-9\%])|www\d{0,3}[.]|[a-z0-9.\-]+[.][a-z]{2,4}\/)(?:[^\s()<>]+|\(([^\s()<>]+|(\([^\s()<>]+\)))*\))+(?:\(([^\s()<>]+|(\([^\s()<>]+\)))*\)|[^\s`!()\[\]{};:'".,<>?<<>>""'']))]T \\
Exemple

\caption{Exemples d'expressions régulières couramment utilisées}\label{tab:regexCourammentUtilisees}

\end{table}

\end{document}

enter image description here

enter image description here

2
  • +1 for a good use of the fvextra package. However, you shouldn't use " as the delimiter character for the final \Verb directive (the URL case), as the " character occurs (several times, in fact) inside the string, toward the very end. Three visual give-aways are (a) the final few characters of the regex string are suddenly rendered in serif glyphs, (b) there's an unnecessarily early line break in the next-to-last row, and (c) the final character of the regex string has somehow become " (instead of ]). Suggestion: write \Verb y...y.
    – Mico
    Oct 21, 2023 at 22:16
  • 1
    @Mico Darn! Too long regex!
    – egreg
    Oct 21, 2023 at 22:22
4

Your code fails for two separate reasons.

  • The character that is used to mark the start and end of the verbatim-character string must not occur in the string itself. However, the delimiter character you employ in the \verb directives -- + -- occurs inside several of the strings.

  • \verb does not allow automatic line breaking. Embedding a \verb directive inside an X-type column will not overcome this.

There are two additional severe problems with your code: First, unless one has access to the file structure.cls, your code won't compile at all. Second, since your tabularx environment doesn't allow automatic line breaking in columns 1 and 3, there is simply no way your table can fit inside the text block.

What to do? First and foremost, I suggest you load the xurl package and replace all \verb/.../ directives with \path{...} directives; this will (a) render the strings in a monospaced font and (b) allow line breaking at arbitary points. Second, do allow automatic line breaking in columns 1 and 3 as well. Third, do use landscape mode to render the table. Fourth, is it permissible to get rid of the instruction \input{structure.cls}? Finally, do consider giving the table a much more open "look" by getting rid of all vertical lines and most horizontal lines.

Aside: The \path macro is just like the \url macro, except that it won't convert its argument to a hyperlink in case the hyperref package is loaded.

enter image description here

\documentclass[11pt]{book}
\usepackage[paperwidth=16cm, paperheight=24cm,
            margin=2cm]{geometry} % set margins as appropriate
\usepackage[T1]{fontenc}
\usepackage[french]{babel}
\usepackage{caption}
\usepackage{tabularx}
\usepackage{ragged2e}

\newcolumntype{L}{>{\RaggedRight}X}
\newcolumntype{P}[1]{>{\RaggedRight}p{#1}}
\usepackage{pdflscape} % for 'landscape' env.
\usepackage{booktabs}  % for well-spaced horizontal rules
\usepackage{xurl}      % for '\path' macro, allow line breaks at arbitrary points

%\input{structure.cls} % % huh?

\begin{document}

\begin{landscape}
\begin{table}[!htbp]     
\setlength\tabcolsep{4pt}  % default: 6pt
% 24cm - 2*2cm = 20cm = available width in landscape mode  
\begin{tabularx}{20cm}{@{} P{3.5cm} L P{3cm} @{}}
    \toprule
      \bfseries Pour trouver 
    & \bfseries RegEx à utiliser 
    & \bfseries Exemple \\
    \midrule
    E-mail 
    & \path{[^[\w\.=-]+@[\w\.-]+\.[\w]{2,3}\$]} 
    & [email protected] \\
    \addlinespace
    Numéros de sécurité sociale américains 
    & \path{[^[a-z]+\$]} 
    & 513-84-7329 \\
    \addlinespace
    Adresses IPV4 
    & \path{[^[\w\.=-]+@[\w\.-]+\.[\w]{2,3}\$]} 
    & 212.23.2.110\\
    \addlinespace
    Dates au format MM/JJ/AAAA 
    & \path{[^\d{1,3}[.]\d{1,3}[.]\d{1,3}[.]\d{1,3}\$]} 
    & Un seul mot non vide en minuscule \\
    \addlinespace
    Numéros de cartes Mastercard 
    & \path{[^(?:5[1-5][0-9]{2}|222[1-9]|22[3-9][0-9]|2[3-6][0-9]{2}|27[01][0-9]|2720)[0-9]{12}\$]} 
    & Exemple \\
    \addlinespace
    Numéros de cartes Visa 
    & \path{[\b([4]\d{3}[\s]\d{4}[\s]\d{4}[\s]\d{4}|[4]\d{3}[-]\d{4}[-]\d{4}[-]\d{4}|[4]\d{3}[.]\d{4}[.]\d{4}[.]\d{4}|[4]\d{3}\d{4}\d{4}\d{4})\b]} 
    & Exemple \\
    \addlinespace
    Numéros de cartes American Express 
    & \path{[^3[47][0-9]{13}\$]} 
    & Exemple \\
    \addlinespace
    Codes ZIP US 
    & \path{[^((\d{5}-\d{4})|(\d{5})|([A-Z]\d[A-Z]\s\d[A-Z]\d))\$]} 
    & Exemple \\
    \addlinespace
    Chemins d’accès à des fichiers 
    & \path{[\\[^\\]+\$]+\$]} 
    & Exemple \\
    \addlinespace
    URL 
    & \path{[(?i)\b((?:[a-z][\w-]+:(?:\/{1,3}|[a-z0-9\%])|www\d{0,3}[.]|[a-z0-9.\-]+[.][a-z]{2,4}\/)(?:[^\s()<>]+|\(([^\s()<>]+|(\([^\s()<>]+\)))*\))+(?:\(([^\s()<>]+|(\([^\s()<>]+\)))*\)|[^\s`!()\[\]{};:'".,<>?<<>>""'']))]} 
    & Exemple \\
    \bottomrule
\end{tabularx}
\caption{Exemples d'expressions régulières couramment utilisées}
\label{tab:regexCourammentUtilisees}
\end{table}
\end{landscape}

\end{document}
4
  • With your code I am getting the following error message ! Package array Error: Illegal pream-token (3cm): `c' used.
    – cProg
    Oct 20, 2023 at 18:17
  • @cProg - The code I posted does not generate errors. The error message you report in the comment suggests that you wrote P(3cm) instead of P{3cm}. Please confirm that you included the instruction \newcolumntype{P}[1]{>{\RaggedRight}p{#1}} in your preamble and that you wrote P{3cm} twice in the second argument of the tabularx environment.
    – Mico
    Oct 20, 2023 at 20:17
  • I have copied your code entirely without changing anything in it. I have updated my question to add the Header, I mean the current preamble of my Latex file.
    – cProg
    Oct 21, 2023 at 20:21
  • @cProg - Your claim that you "copied [my] code entirely without changing anything in it" is incorrect. My code has \newcolumntype{L}{>{\RaggedRight}X}, whereas yours has \newcolumntype{L}[1]{>{\RaggedRight}X}. By the way, it makes no sense to define the L column type to take an argument (an intended width, I would assume?) and then discard that argument. In fact, your definition is an open invitation to disaster -- which, sure enough, has occurred. The upshot? Do please use my setting, viz., \newcolumntype{L}{>{\RaggedRight}X}
    – Mico
    Oct 21, 2023 at 20:57
4

There is no problems with verbatim in table when using OpTeX:

\fontfam[lm]

\thistable{\verbchar X \def\_printinverbatim #1{\foreach#1\do{##1\hskip0ptplus1pt\relax}}}
\table pxto\hsize {|p{4cm\fL}|p{\tsize}|p{3cm\fL}|}{\crl
   \bf Pour trouver & \bf RegEx à utiliser & \bf Exemple \crl
    E-mail & X[^[\w\.=-]+@[\w\.-]+\.[\w]{2,3}\$]X & [email protected] \crl
    Numéros de sécurité sociale américains & X[^[a-z]+\$]X & 513-84-7329 \crl
    Adresses IPV4 & X[^[\w\.=-]+@[\w\.-]+\.[\w]{2,3}\$]X & 212.23.2.110 \crl
    Dates au format MM/JJ/AAAA & X[^\d{1,3}[.]\d{1,3}[.]\d{1,3}[.]\d{1,3}\$]X & Un seul mot non vide en minuscule \crl
    Numéros de cartes Mastercard & X[^(?:5[1-5][0-9]{2}|222[1-9]|22[3-9][0-9]|2[3-6][0-9]{2}|27[01][0-9]|2720)[0-9]{12}\$]X & Exemple \crl
    Numéros de cartes Visa & X[\b([4]\d{3}[\s]\d{4}[\s]\d{4}[\s]\d{4}|[4]\d{3}[-]\d{4}[-]\d{4}[-]\d{4}|[4]\d{3}[.]\d{4}[.]\d{4}[.]\d{4}|[4]\d{3}\d{4}\d{4}\d{4})\b]X & Exemple \crl
    Numéros de cartes American Express & X[^3[47][0-9]{13}\$]X & Exemple \crl
    Codes ZIP US & X[^((\d{5}-\d{4})|(\d{5})|([A-Z]\d[A-Z]\s\d[A-Z]\d))\$]X & Exemple \crl
    Chemins d’accès à des fichiers & X[\\[^\\]+\$]+\$]X & Exemple \crl
    URL & X[(?i)\b((?:[a-z][\w-]+:(?:\/{1,3}|[a-z0-9\%])|www\d{0,3}[.]|[a-z0-9.\-]+[.][a-z]{2,4}\/)(?:[^\s()<>]+|\(([^\s()<>]+|(\([^\s()<>]+\)))*\))+(?:\(([^\s()<>]+|(\([^\s()<>]+\)))*\)|[^\s`!()\[\]{};:'".,<>?<<>>""'']))]X & Exemple \crl
}
\bye

table compiled with column of verbatim content in OpTeX

4
  • I get an error when I try to compile this. ! Missing $ inserted. <inserted text> $ <to be read again> ^ l.1 ...4}) Numéros de cartes American Express & X[^
    – cfr
    Oct 22, 2023 at 5:31
  • Something lost from the Numéros de cartes Visa and URL lines, maybe? If I comment those, it's fine. I don't know anything about OpTeX, but the Visa line surely needs a \crl, even if the error is occurring before TeX gets that far.
    – cfr
    Oct 22, 2023 at 5:38
  • @cfr yes, you are right. It was my problem with copy-pasting of very width line.
    – wipet
    Oct 22, 2023 at 17:37
  • Yes, I edited when I realised it was copy-pasting and I could therefore infer what was missing and get it working.
    – cfr
    Oct 22, 2023 at 20:00

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .