【问题标题】:Finding Matching Strings Within Paragraphs在段落中查找匹配的字符串
【发布时间】:2021-03-30 20:22:42
【问题描述】:

我有一个包含 LaTeX 数学方程式的 TXT 文件,其中在每个内联方程式之前和之后使用单个 $ 分隔符。

我想在一个段落中找到每个方程,并用 XML 开始和结束标记替换分隔符 ....

例如,

以下段落:

This is the beginning of a paragraph $first equation$ ...and here is some text... $second equation$ ...and here is more text... $third equation$ ...and here is yet more text... $fourth equation$

应该变成:

This is the beginning of a paragraph <equation>first equation</equation> ...and here is some text... <equation>second equation</equation> ...and here is more text... <equation>third equation</equation> ...and here is yet more text... <equation>fourth equation</equation>

我已经尝试过如下的 sed 和 perl 命令:

perl -p -e 's/(\$)(.*[^\$])(\$)/<equation>$2<\/equation>/'

但是这些命令会导致方程的第一个和最后一个实例被转换,但没有这两个方程之间的方程:

This is the beginning of a paragraph <equation>first equation$ ...and here is some text... $second equation$ ...and here is more text... $third equation$ ...and here is yet more text... $fourth equation</equation>

我还想要一个强大的解决方案,它可以考虑到不用作 LaTeX 分隔符的单个 $ 的存在。例如,

This is the beginning of a paragraph $first equation$ ...and here is some text that includes a single dollar sign: He paid $2.50 for a pack of cigarettes... $second equation$ ...and here is more text... $third equation$ ...and here is yet more text... $fourth equation$

不会变成:

This is the beginning of a paragraph <equation>first equation$ ...and here is some text that includes a single dollar sign: He paid <equation>2.50 for a pack of cigarettes... $second equation$ ...and here is more text... $third equation$ ...and here is yet more text... $fourth equation</equation>

注意:我正在用 Bash 写作。

【问题讨论】:

  • 我对LaTeX不熟悉,但我猜公式里面没有空格吧?
  • @PedroMaimere LaTeX 数学表达式的$...$ 内可以有空格。
  • 有什么东西可以触发美元符号(不)是否属于公式?
  • 很遗憾没有,我想不到。有时在第一个 $ 之后会立即出现 LaTeX 命令(例如 \frac),但并非总是如此。有时只有数字、括号或文本,所有这些都可以在用于不同目的的 $ 之后找到...... LaTeX 方程与 $ 的其他用途的区别在于,方程总是包含在 $ 中,而单个 $ 可以用于其他目的。在这种情况下,很难排除误报。
  • 粗略搜索 (la)tex、美元符号、方程式...找到了一些参考资料,也许 OP 可以评论:1) 使用 \(...\) 而不是 @987654329 @ 指定方程,2) 转义独立的$(即\$)以将其指定为文字$;这些(现实的)选项中的任何一个是否可用于帮助确定应如何处理$

标签: regex perl awk sed


【解决方案1】:

注意:此答案的第一部分仅关注替换成对的$'s;对于 OP 要求 替换独立 $'s ... 请参阅答案的第二半。


替换成对的$'s

样本数据:

$ cat latex.txt
... $first equation$ ... $second equation$ ... $third equation$

一个sed想法:

sed -E 's|\$([^$]*)\$|<equation>\1</equation>|g' latex.txt

地点:

  • -E - 启用扩展的正则表达式支持
  • \$ - 匹配文字 $
  • ([^$]*) - [捕获组 #1] - 匹配非文字 $ 的所有内容(在这种情况下,$'s 对之间的所有内容)
  • \$ - 匹配文字 $
  • &lt;equation&gt;\1&lt;/equation&gt; - 将匹配的字符串替换为 &lt;equation&gt; + contents of capture group + &lt;/equation&gt;
  • /g - 根据需要重复搜索/替换

这会生成:

... <equation>first equation</equation> ... <equation>second equation</equation> ... <equation>third equation</equation>

处理独立的$

如果可以转义独立的$(例如\$),一个想法是让sed 将其替换为无意义的文字,执行&lt;equation&gt; / &lt;/equation&gt; 替换,然后将无意义的文字改回@ 987654346@.

样本数据:

$ cat latex.txt
... $first equation$ ... $second equation$ ... $third equation$
... $first equation$ ... \$3.50 cup of coffee ... $third equation$

原来的sed 解决方案与新的替代品:

sed -E 's|\\\$|LITDOL|g;s|\$([^$]*)\$|<equation>\1</equation>|g;s|LITDOL|\\\$|g' latex.txt

我们将\$ 替换为LITDOL (LITeral DOLlar),执行我们原来的替换,然后将LITDOL 切换回\$

生成:

... <equation>first equation</equation> ... <equation>second equation</equation> ... <equation>third equation</equation>
... <equation>first equation</equation> ... \$3.50 cup of coffee ... <equation>third equation</equation>

【讨论】:

  • 谢谢!我能够将这行代码插入到我的脚本中,并且工作正常。感谢您的时间和精力。
  • 使用 sed 时,正常的“输入中不存在的东西”是 \n,因此您可以使用它而不是 LITDOL,因此您不必担心它会出现在输入。
【解决方案2】:

使用负前瞻试试这个 Perl。

$ cat joseph.txt
This is the beginning of a paragraph $first equation$ ...and here is some text that includes a single dollar sign: He paid $2.50 for a pack of cigarettes... $second equation$ ...and here is more text... $third equation$ ...and here is yet more text... $fourth equation$
$ perl -p -e 's/(\$)(?![\d.]+)(.+?)(\$)/<equation>$2<\/equation>/g' joseph.txt
This is the beginning of a paragraph <equation>first equation</equation> ...and here is some text that includes a single dollar sign: He paid $2.50 for a pack of cigarettes... <equation>second equation</equation> ...and here is more text... <equation>third equation</equation> ...and here is yet more text... <equation>fourth equation</equation>
$

【讨论】:

  • 这也是LaTeX方程$2+a$上面的代码会发生什么?
  • (?![\d.]+)\s 一个额外的空间可以工作
  • 等式中的空格无关紧要可能是$ 2 + A + B $$2AB$ 我不熟悉LaTeX 只是澄清一下,可能是OP 在您的回答中取得了很好的成功。
  • @ssr1012.. 是的,对...这很棘手!.. 不确定 LateX 是如何描述的。
猜你喜欢
  • 2014-10-22
  • 1970-01-01
  • 1970-01-01
  • 2021-06-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-11-30
  • 2015-07-14
相关资源
最近更新 更多