如何在 LaTeX 表中排版正则表达式字符串?

如何在 LaTeX 表中排版正则表达式字符串?

我想排版一个表格,在表格的一列中使用正则表达式。我想放入表格中的数据如下:

在此处输入图片描述

我已尝试使用以下代码:

\newcolumntype{e}{>{\hsize=0.02\hsize}X}
\newcolumntype{s}{>{\hsize=0.18\hsize}X}
\newcolumntype{b}{>{\hsize=0.80\hsize}X}
\begin{table} [!htb]
%\small
\footnotesize
\caption{List of Regex Patterns to filter GitHub repositories.}
\label{regex-list}
\begin{tabularx}{\columnwidth} {|e | s | b |}
 \hline
 \multicolumn{1}{|c|}{\textbf{ID}} &
  \multicolumn{1}{c|}{\textbf{Secret Type}} &
  \multicolumn{1}{c|}{\textbf{Regular Expression}}\\
 \hline \hline
 65 & AWS API Secret & \b([A-Za-z0-9+/]{40})[ \r\n'"\x60] \\ \hline
 71 & Azure Client Secret & (?i)(%s).{0,20}([a-z0-9_\.\-~]{34}) \\ \hline
 278 & Generic Pattern & (?i)(?:pass|token|cred|secret|key)(?:.|[\n\r]){0,40}(\b[\x21-\x7e]{16,64}\b) \\ \hline
 605 & Slack Token &   (xoxb$\vert$xoxp$\vert$xapp$\vert$xoxa$\vert$xoxr)-[0-9]10,13[a-zA-Z0-9]* \\ \hline
 640 & Stripe API Key & [rs]k_live_[a-zA-Z0-9]{20,30} \\ \hline
\end{tabularx}
\end{table}

但是,表格被损坏并且正则表达式无法正确显示。

在此处输入图片描述

然后我尝试添加如下所示的 \verb 命令:

\newcolumntype{e}{>{\hsize=0.02\hsize}X}
\newcolumntype{s}{>{\hsize=0.18\hsize}X}
\newcolumntype{b}{>{\hsize=0.80\hsize}X}
\begin{table} [!htb]
%\small
\footnotesize
\caption{List of Regex Patterns to filter GitHub repositories.}
\label{regex-list}
\begin{tabularx}{\columnwidth} {|e | s | b |}
 \hline
 \multicolumn{1}{|c|}{\textbf{ID}} &
  \multicolumn{1}{c|}{\textbf{Secret Type}} &
  \multicolumn{1}{c|}{\textbf{Regular Expression}}\\
 \hline \hline
 65 & AWS API Secret & \verb/\b([A-Za-z0-9+/]{40})[ \r\n'"\x60]/ \\ \hline
 71 & Azure Client Secret & \verb/(?i)(%s).{0,20}([a-z0-9_\.\-~]{34})/ \\ \hline
 278 & Generic Pattern & \verb/(?i)(?:pass|token|cred|secret|key)(?:.|[\n\r]){0,40}(\b[\x21-\x7e]{16,64}\b)/ \\ \hline
 605 & Slack Token &  \verb/(xoxb$\vert$xoxp$\vert$xapp$\vert$xoxa$\vert$xoxr)-[0-9]10,13[a-zA-Z0-9]*/ \\ \hline
 640 & Stripe API Key & \verb/[rs]k_live_[a-zA-Z0-9]{20,30}/ \\ \hline
\end{tabularx}
\end{table}

但是,正则表达式仍然没有正确显示。

在此处输入图片描述

在 LaTeX 表中排版正则表达式的最佳方法是什么?

答案1

我建议您(a)加载该xurl包并使用其\path宏来排版正则表达式字符串,以及(b)\catcode 37=11先执行\begin{tabularx}以删除符号的 TeX 特殊性质%(ASCII 代码:37)。

的参数\path可以在任意位置断行。大多数 TeX 特殊字符(包括{}\(反斜杠)和_(下划线))都可以通过 轻松处理\path。据我所知,唯一会在 的参数中引起混乱的 TeX 特殊字符\path%。这就是为什么必须执行上述步骤 (b) 的原因——当然,除非所有正则表达式字符串都不包含%开头的字符。(但是,在您的表中,该%字符确实出现。)

请注意,由于 TeX 特殊含义%被上述操作 (b) 禁用,因此 范围内不允许使用 TeX 样式的注释\catcode 37=11。此处,范围结束于\end{table}

在此处输入图片描述

\documentclass{article}
\usepackage{tabularx}
\usepackage{xurl} % allow line breaks at arbitrary locations
\begin{document}

\begin{table} [!htb]
\setlength\extrarowheight{2pt} % for a less cramped "look"
\caption{List of Regex Patterns to filter GitHub repositories.\strut}
\label{regex-list}
% Assign category code 11 ("other") to "%" symbol:
\catcode 37=11
\begin{tabularx}{\columnwidth} {| l | l | X |}
 \hline
 ID & Secret Type & Regular Expression \\
 \hline \hline
 65 & AWS API Secret & \path{\b([A-Za-z0-9+/]{40})[ \r\n'"\x60]} \\ 
 \hline
 71 & Azure Client Secret & \path{(?i)(%s).{0,20}([a-z0-9_\.\-~]{34})} \\ 
 \hline
 278 & Generic Pattern & \path{(?i)(?:pass|token|cred|secret|key)(?:.|[\n\r]){0,40}(\b[\x21-\x7e]{16,64}\b)} \\ 
 \hline
 605 & Slack Token & \path{(xoxb|xoxp|xapp|xoxa|xoxr)-[0-9]10,13[a-zA-Z0-9]*} \\ 
 \hline
 640 & Stripe API Key & \path{[rs]k_live_[a-zA-Z0-9]{20,30}} \\ 
 \hline
\end{tabularx}
\end{table}

\end{document}

答案2

为了学习目的,我会指出您最初的尝试中出了什么问题。

尝试 1. 手动转义字符

你需要逃跑全部字符,对于管道符号,可以使用T1字体编码(推荐!)或手动转义它们。

參閱

无论如何,以下代码有效

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


\newcolumntype{e}{>{\hsize=0.02\hsize}X}
\newcolumntype{s}{>{\hsize=0.18\hsize}X}
\newcolumntype{b}{>{\hsize=0.80\hsize}X}
\begin{table} [!htb]
%\small
\footnotesize
\caption{List of Regex Patterns to filter GitHub repositories.}
\begin{tabularx}{\columnwidth} {|e | s | b |}
 \hline
 \multicolumn{1}{|c|}{\textbf{ID}} &
  \multicolumn{1}{c|}{\textbf{Secret Type}} &
  \multicolumn{1}{c|}{\textbf{Regular Expression}}\\
 \hline \hline
 65 & AWS API Secret & \textbackslash b([A-Za-z0-9+/]\{40\})[ \textbackslash r\textbackslash n'"\textbackslash x60] \\ \hline
 71 & Azure Client Secret & (?i)(\%s).\{0,20\}([a-z0-9\_\textbackslash.\textbackslash-\~{}]\{34\}) \\ \hline
 278 & Generic Pattern & (?i)(?:pass|token|cred|secret|key)(?:.|[\textbackslash n\textbackslash r])\{0,40\}(\textbackslash b[\textbackslash x21-\textbackslash x7e]\{16,64\}\textbackslash b) \\ \hline
 605 & Slack Token &   (xoxb$\vert$xoxp$\vert$xapp$\vert$xoxa$\vert$xoxr)-[0-9]10,13[a-zA-Z0-9]* \\ \hline
 640 & Stripe API Key & [rs]k\_live\_[a-zA-Z0-9]\{20,30\} \\ \hline
\end{tabularx}
\end{table}


\begin{table} [!htb]
%\small
\footnotesize
\caption{List of Regex Patterns to filter GitHub repositories.}
\begin{tabularx}{\columnwidth} {|e | s | b |}
 \hline
 \multicolumn{1}{|c|}{\textbf{ID}} &
  \multicolumn{1}{c|}{\textbf{Secret Type}} &
  \multicolumn{1}{c|}{\textbf{Regular Expression}}\\
 \hline \hline
  65 & AWS API Secret & \texttt{\textbackslash b([A-Za-z0-9+/]\{40\})[ \textbackslash r\textbackslash n'"\textbackslash x60]} \\ \hline
71 & Azure Client Secret & \texttt{(?i)(\%s).\{0,20\}([a-z0-9\_\textbackslash.\textbackslash-\~{}]\{34\})} \\ \hline
278 & Generic Pattern & \texttt{(?i)(?:pass|token|cred|secret|key)(?:.|[\textbackslash n\textbackslash r])\{0,40\}\discretionary{}{}{}(\textbackslash b[\discretionary{}{}{}\textbackslash x21-\textbackslash x7e]\{16,64\}\textbackslash b)} \\ \hline
605 & Slack Token &   \texttt{(xoxb$\vert$xoxp$\vert$xapp$\vert$xoxa$\vert$xoxr)-[0-9]10,13[a-zA-Z0-9]*} \\ \hline
     640 & Stripe API Key & \texttt{[rs]k\_live\_[a-zA-Z0-9]\{20,30\}} \\ \hline
\end{tabularx}
\end{table}


\end{document}

尝试2.使用\verb

然而,正确地转义所有这些字符是非常困难的。因此,你可以尝试使用\verb

然而,

\begin{tabularx}{2cm}{c}
\verb|a|
\verb|\\\a\b|  % some spaces are managed
%\verb!a!  % breaks!
\verb/a/
\end{tabularx}

无论如何,为了解决这个\verb问题,我使用我的包cprotectinside你必须通过撤消黑客攻击tabularx,所以最终结果是

\documentclass{article}
\usepackage{tabularx}
\usepackage{cprotectinside}
\begin{document}

\newcolumntype{e}{>{\hsize=0.02\hsize}X}
\newcolumntype{s}{>{\hsize=0.18\hsize}X}
\newcolumntype{b}{>{\hsize=0.80\hsize}X}

\let\normalverb\verb

\cprotectinside{@}{
\begin{table} [!htb]
%\small
\footnotesize
\caption{List of Regex Patterns to filter GitHub repositories.}
\begin{tabularx}{\columnwidth} {|e | s | b |}
 \hline
 \multicolumn{1}{|c|}{\textbf{ID}} &
  \multicolumn{1}{c|}{\textbf{Secret Type}} &
  \multicolumn{1}{c|}{\textbf{Regular Expression}}\\
 \hline \hline


 65 & AWS API Secret     & @\normalverb`\b([A-Za-z0-9+/]{40})[ \r\n'"\x60]`@ \\ \hline
 71 & Azure Client Secret& @\normalverb`(?i)(%s).{0,20}([a-z0-9_\.\-~]{34})`@ \\ \hline
 278& Generic Pattern    & @\normalverb`(?i)(?:pass|token|cred|secret|key)(?:.|[\n\r]){0,40}(\b`\discretionary{}{}{}\normalverb`[\x21-\x7e]{16,64}\b)`@ \\ \hline
 605& Slack Token        & @\normalverb`(xoxb|xoxp|xapp|xoxa|xoxr)-[0-9]10,13[a-zA-Z0-9]*`@ \\ \hline
 640& Stripe API Key     & @\normalverb`[rs]k_live_[a-zA-Z0-9]{20,30}`@ \\ \hline
\end{tabularx}
\end{table}
}

\end{document}

顺便说一句,要小心警告。不建议重新定义b...

Package array Warning: Redefining primitive column b on input line 8.

但这不是我们关心的问题。

选择:lstinline

您仍然需要逃避某些事情,但这比标准逃避更容易。

\documentclass{article}
\usepackage{tabularx}
\usepackage{listings}
\begin{document}

\newcolumntype{e}{>{\hsize=0.02\hsize}X}
\newcolumntype{s}{>{\hsize=0.18\hsize}X}
\newcolumntype{b}{>{\hsize=0.80\hsize}X}

\let\normalverb\verb

% set font of lstinline to use texttt font
\lstset{basicstyle=\ttfamily}

\begin{table} [!htb]
%\small
\footnotesize
\caption{List of Regex Patterns to filter GitHub repositories.}
\begin{tabularx}{\columnwidth} {|e | s | b |}
 \hline
 \multicolumn{1}{|c|}{\textbf{ID}} &
  \multicolumn{1}{c|}{\textbf{Secret Type}} &
  \multicolumn{1}{c|}{\textbf{Regular Expression}}\\
 \hline \hline


 65 & AWS API Secret     & \lstinline`\\b([A-Za-z0-9+/]\{40\})[ \\r\\n'"\\x60]` \\ \hline
 71 & Azure Client Secret& \lstinline`(?i)(\%s).\{0,20\}([a-z0-9_\\.\\-~]\{34\})` \\ \hline
 278& Generic Pattern    & \lstinline`(?i)(?:pass|token|cred|secret|key)(?:.|[\n\r])\{0,40\}(\b`\discretionary{}{}{}\lstinline`[\\x21-\\x7e]\{16,64\}\\b)` \\ \hline
 605& Slack Token        & \lstinline`(xoxb|xoxp|xapp|xoxa|xoxr)-[0-9]10,13[a-zA-Z0-9]*` \\ \hline
 640& Stripe API Key     & \lstinline`[rs]k_live_[a-zA-Z0-9]\{20,30\}` \\ \hline
\end{tabularx}
\end{table}

\end{document}

答案3

您可以使用软件包\regexp{}附带的命令biblatex

\documentclass{article}
\usepackage{tabularx}
\usepackage{biblatex}
\begin{document}

\newcolumntype{e}{>{\hsize=0.02\hsize}X}
\newcolumntype{s}{>{\hsize=0.18\hsize}X}
\newcolumntype{b}{>{\hsize=0.80\hsize}X}
\begin{table} [!htb]
%\small
\footnotesize
\caption{List of Regex Patterns to filter GitHub repositories.}
\label{regex-list}
\begin{tabularx}{\columnwidth} {|e | s | b |}
 \hline
 \multicolumn{1}{|c|}{\textbf{ID}} &
  \multicolumn{1}{c|}{\textbf{Secret Type}} &
  \multicolumn{1}{c|}{\textbf{Regular Expression}}\\
 \hline \hline
 65 & AWS API Secret & \regexp{\b([A-Za-z0-9+/]{40})[ \r\n'"\x60]} \\ \hline
 71 & Azure Client Secret & \regexp{(?i)(\%s).{0,20}([a-z0-9_\.\-~]{34})} \\ \hline
 278 & Generic Pattern & \regexp{(?i)(?:pass|token|cred|secret|key)(?:.|[\n\r]){0,40}(\b[\x21-\x7e]{16,64}\b)} \\ \hline
 605 & Slack Token & \regexp{(xoxb|xoxp|xapp|xoxa|xoxr)-[0-9]10,13[a-zA-Z0-9]*} \\ \hline
 640 & Stripe API Key & \regexp{[rs]k_live_[a-zA-Z0-9]{20,30}} \\ \hline
\end{tabularx}
\end{table}


\end{document}

唯一需要转义的字符是%biblatex 文档

可以使用 Perl 转义序列,例如 \t 表示制表符、\n 表示换行符、\A 表示字符串开头或 \d 表示数字,而 TEX 不会尝试将它们作为命令执行,特殊字符如 ^、_ 或 {..} 和 # 也可以。只有 % 必须受到保护:要匹配 bib 中的单个 %,请在正则表达式中使用 %,\% 可匹配 %

相关内容