正则将此问题格式转换为 LaTeX

正则将此问题格式转换为 LaTeX
1. Lorem ipsun la la la?
1. Sopor
2. Stupor
3. Torpor

2. A patient has Lorem?
1. Sopor
2. Stupor
3. Somnolentia 
4. La
5. Coma

3. Doesn't Response to strong external irritants is short. Tendon, pupillary, corneal reflexes are retained. Doesn't Response to strong external irritants is short. Tendon, pupillary, corneal reflexes are retained. What disorder of consciousness does he have?
1. Stupor
2. Sopor
3. Somnolentia 
4. Euphoria
5. Coma

... [777 questions]

我想成为

l. Lorem ipsun la la la?
\begin{enumerate}
\item Sopor
\item Stupor
\item Torpor
\end{enumerate}    

2. A patient has Lorem?
\begin{enumerate}
\item Sopor
\item Stupor
\item Somnolentia 
\item La
\item Coma
\end{enumerate}

3. Doesn't Response to strong external irritants is short. Tendon, pupillary, corneal reflexes are retained. Doesn't Response to strong external irritants is short. Tendon, pupillary, corneal reflexes are retained. What disorder of consciousness does he have?
\begin{enumerate}
\item Stupor
\item Sopor
\item Somnolentia 
\item Euphoria
\item Coma
\end{enumerate}

一些笔记

  • 答案有 3 到 5 个选项
  • 我不确定执行这些更改的最佳顺序。
  • 列表1.[ A-Za-z123345679.]*5.\n应替换为\n\begin{enumerate}[match]\end{enumerate}\n
    • 如果不成功, 1.[ A-Za-z123345679.]*4.\n则应替换为\n\begin{enumerate}[mathch]\end{enumerate}\n
    • 如果不成功,1.[ A-Za-z123345679.]*3.\n则应替换为\n\begin{enumerate}[mathch]\end{enumerate}\n

我应该使用什么命令行工具来执行此操作?我想到了 Perl,但我不确定。


我刚刚注意到,捕捉文件内容会产生与查看器中预期不同的输出。我目前使用的是带有 Perl v5.16.2 的最新 OSX。

这是测试文件

输入、命令和输出

$ cat questions_copy.tex 
1. Lorem ipsun la la la?
1. Sopor
2. Stupor
3. Torpor

2. A patient has Lorem?
1. Sopor
2. Stupor
3. Somnolentia 
4. La
5. Coma

% STRANGE cat output here - Not correct!
3. Doesn't Response to strong external irritants is short. Tendon, pupillary, corneal reflexes are retained. Doesn't Response to strong external irritants is short. Tendon, 3. Somnolentia eal reflexes are retained. What disorder of consciousness does he have?
5. Comaoria
% PERL do the same mistakes
$ perl -000pe 's/\n/\n\\begin{enumerate}\n/; s/\n\d./\n\\item /g; s/$/\\end{enumerate}\n/' questions_copy.tex 
1. Lorem ipsun la la la?
\begin{enumerate}
\item  Sopor
\item  Stupor
\item  Torpor
\end{enumerate}

2. A patient has Lorem?
\begin{enumerate}
\item  Sopor
\item  Stupor
\item  Somnolentia 
\item  La
\item  Coma
\end{enumerate}

3. Doesn't Response to strong external irritants is short. Tendon, pupillary, corneal reflexes are retained. Doesn't Response to strong external irritants is short. Tendon, 3. Somnolentia eal reflexes are retained. What disorder of consciousness does he have?
\begin{enumerate}
5. Coma\end{enumerate}

$ 

答案1

这是一种方法。这假设问题由连续的换行符 ( ) 分隔\n\n

$ perl -000pe 's/\n/\n\\begin{enumerate}\n/; 
                s/\n\d./\n\\item /g; s/$/\\end{enumerate}\n/' file 
l. Lorem ipsun la la la?
\begin{enumerate}
\item  Sopor
\item  Stupor
\item  Torpor
\end{enumerate}

2. A patient has Lorem?
\begin{enumerate}
\item  Sopor
\item  Stupor
\item  Somnolentia 
\item  La
\item  Coma\end{enumerate}

解释

  • -000: 激活 Perl 的段落模式,这会导致“行”由两个连续的换行符 ( ) 定义\n\n,以便您的每个问题都被视为一行。
  • -pne:读取输入文件的每一行并-p在应用传递为 的脚本后打印它 () -e
  • s/\n/\n\\begin{enumerate}\n/:用 替换该行(问题)的第一个换行符\begin{enumerate}\n
  • s/\n\d./\n\\item /gg:用换行符替换换行符后面的任何 ( ) 数字,\item后跟一个空格。
  • s/$/\\end{enumerate}\n/':用换行符替换记录末尾 ( $) 。\end{enumerate}

相关内容