在 Perl 中解析 Latex 文件
对于这个非常基本的问题表示歉意!
我只想读取乳胶文件(基本上是文本)并输出所有(比如说)定理,这些定理始终采用
\begin{theorem}
some lines of latex
\end{theorem}
我总是认为 Perl 是正确语言的格式!
当然,我只懂非常基础的C++和Java编程,几乎不懂Perl。
尽管如此,我目前可以读取文本文件,并逐行处理它。
似乎最基本的方法是:
($string =~ /pattern/)
通过阅读 ?、*+、$ 等控制代码,我开始感到困惑。
有任何简单的参考或链接可以让我开始吗?
(我把它放在这里而不是 Tex 站点,因为它通常对于读取文本文件很有用,而不仅仅是 LaTeX!)
Apologies for the very basic question!
I just want to read in a latex file (so text basically) and output all the (say) theorems, which are always in the format
\begin{theorem}
some lines of latex
\end{theorem}
I always kind of figured Perl was the right language for this!
Of course, I only know very basic programming in C++ and Java, and virtually no Perl.
Nonetheless I can currently read in a text file, and process it line by line.
It seems the most basic way to do this is:
($string =~ /pattern/)
I started getting confused by then reading about control codes like ?,*+,$, etc.
Any simple references or links to get me started?
(I put this on here and not the Tex site, as it could be useful generally for reading text files, and not just LaTeX!)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您使用的是 Unix-y 机器(包括 Mac),对于这么小的任务,您应该首先使用
sed
:但是,如果您使用的是 Windows,则不会得到 < code>sed 与操作系统捆绑在一起,而 perl 更容易安装 AIUI,所以这里是等效的:
您可能会注意到这两个命令之间有明显的相似之处。这并非偶然。 Perl 从许多旧的 Unix 文本处理实用程序(包括 sed)中汲取了灵感。
If you're on a Unix-y machine (this includes Macs), for a task this small you should reach for
sed
first:If you're on Windows, though, you don't get
sed
bundled with the OS, and perl is rather easier to install AIUI, so here's the equivalent:You may notice a distinct resemblance between these two commands. That's not an accident; Perl took ideas from many of the older Unix text-munging utilities,
sed
included.我认为模式
\begin{theorem}(.*)\end{theorem}
匹配后,你的定理将位于变量 $1 中。
注意:我在这里假设最基本的结构。如果定理可以是多行的,那么我可以给你更好的解决方案。
I think the pattern
\begin{theorem}(.*)\end{theorem}
and then your theorem will be in variable $1 after you do the matching.
Note: I am assuming the most basic structure here. If theorem can be multi-line then I can give you the better solution.