【问题标题】:Find and replace a specific reference pattern by a regular expression通过正则表达式查找和替换特定的参考模式
【发布时间】:2015-04-19 12:44:32
【问题描述】:

对于按时间顺序排列的索引 (https://tex.stackexchange.com/questions/239006/index-in-chronological-order-with-a-predefined-non-alphabetical-index-list/239200#239200) 我想替换以下条目(以及我的文档中的更多此类类型),以便(数字,如果存在,加上)前三个字母字母写在第一个括号中。第二个括号必须以数字开头,数字后面可以跟其他东西。例子:

\index[bibel]{Psalm!Psalm 102,26}                 => \bibelindex{Psa}{102,26}
\index[bibel]{1. Johannes!1. Johannes 4,16}       => \bibelindex{1Joh}{4,16}
\index[bibel]{Offenbarung!Offenbarung 21,16b}     => \bibelindex{Off}{21,16b}
\index[bibel]{Habakuk!Habakuk 3,17f.}             => \bibelindex{Hab}{3,17f.}
\index[bibel]{Kolosser!Kolosser 4,3-6}            => \bibelindex{Kol}{4,3-6}
\index[bibel]{1. Korinther!1. Korinther 15,20-28} => \bibelindex{1Kor}{15,20-28}
\index[bibel]{1. Korinther!1. Korinther 15,50.51} => \bibelindex{1Kor}{15,50.51}
\index[bibel]{Psalm!Psalm 2,2.7.9.17.19.23}       => \bibelindex{Psa}{2,2.7.9.17.19.23}
\index[bibel]{Lukas!Lukas 21,11.16-19}            => \bibelindex{Luk}{21,11.16-19}
\index[bibel]{Markus!Markus 24,35-37.40-43.45f.}  => \bibelindex{Mar}{24,35-37.40-43.45f.}

是否可以使用单个搜索模式+单个替换模式和正则表达式来一步替换所有示例?

编辑

编辑 1:我忘了提到我还有其他不应更改的文本:

\index[stichwort]{Begriffe!Zeichen} => \index[stichwort]{Begriffe!Zeichen}
\index[stichwort]{Bilder [wörtl./bildhaft:Gleichnis,Symbol/beides]!Personen!Abraham} => \index[stichwort]{Bilder [wörtl./bildhaft:Gleichnis,Symbol/beides]!Personen!Abraham}

编辑 2:我忘了提,括号内还有 ä/ö/ü 字符:

\index[bibel]{Römer!Römer 6,14.15}\nobreakword{(Römer 6,14.15)} => \bibelindex{Röm}{6,14.15}\nobreakword{(Römer 6,14.15)}
\index[bibel]{Matthäus!Matthäus 15,29-31}\nobreakword{(Matthäus 15,29-31)} => \bibelindex{Mat}{15,29-31}\nobreakword{(Matthäus 15,29-31)}

编辑 3:是否也可以包括其中数字部分写在下一行的模式(因为复制粘贴)在文本和数字之间有一个 ENTER(空格/许多空白字符),例如:

\index[bibel]{1. 
Korinther!1. Korinther 15,20-28} => \bibelindex{1Kor}{15,20-28}

\index[bibel]{1. Korinther!1. 
Korinther 15,20-28} => \bibelindex{1Kor}{15,20-28}

\index[bibel]{1. Korinther!1. 
    Korinther 15,20-28} => \bibelindex{1Kor}{15,20-28}

\index[bibel]{1. Korinther!1. Korinther 
        15,20-28} => \bibelindex{1Kor}{15,20-28}

编辑 4:

事件虽然正则表达式页面完美运行,但 https://regex101.com/r/cP1kT6/2 不幸的是有 2 秒的限制,这对我来说还不够,因为我有一个包含 3000 行和大约 200 个条目的文本要替换。

.*!(\d?)(.\s)?(\w{3})\w*\s([\d\w.\-,]*)}\\bibelindex{$2$3}{$4} 在 RegExpEditor(因为我需要一个 Windows 免费软件)中给出消息:“无效的起始偏移”和 .*{((\d+)(?:\.\s+))?(\w{3}).*?([a-z0-9.,-]+)} 给出“索引 1 附近的无效重复”。

使用适用于 Windows 的免费软件“Notepad++”会导致:“查找:无效的正则表达式”

您知道在 Windows 下运行的免费软件,还是在具有正则表达式搜索和替换功能的普通程序中运行的正则表达式?

【问题讨论】:

  • 请展示您期望的输出应该是什么样子。
  • 它应该看起来像 =>

标签: regex replace find


【解决方案1】:

您可以使用\\index\[bibel\]{((\d+)(?:\.\s+))?(.{3})[\s\S]*?([a-z0-9.,-]+)} 来查找匹配项。

并将匹配替换为\\bibelindex{$2$3}{$4}

编辑:根据您的编辑 1,2 和 3 更新正则表达式。

注意:如果您正在测试多行,您可以通过更改右上角的设置来增加 regex101 上的max execution time

DEMO

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-04-30
    • 2014-10-20
    • 1970-01-01
    相关资源
    最近更新 更多