用正则表达式查找并替换特定的参考模式 [英] Find and replace a specific reference pattern by a regular expression

查看:264
本文介绍了用正则表达式查找并替换特定的参考模式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有关按时间指数( 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。约翰内斯!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:我忘了提及我有其他的文本不应该改变:

$ $ p $ $ $ $ c> \ index [stichwort] {Begriffe!Zeichen} => \index [stichwort] {!Begriffe贼臣}
\index [stichwort] {BILDER [wörtl/ bildhaft:Gleichnis,符号/ beides]!Personen亚伯拉罕} => \index [stichwort] {BILDER [wörtl/ bildhaft:Gleichnis,符号/ beides]!Personen亚伯拉罕}

编辑2:我忘了提及,我也有ä/ö/ \ index [bibel] {Römer!Römer6,14.15} \\\\\\\\\\\\\\\\\\\\\\\\\\ \bibelindex {ROM} {6,14.15} \\\
obreakword {(罗梅尔6,14.15)}
\index [比贝尔] {马特乌斯!马特乌斯15,29-31} \\\
obreakword {(马特乌斯15 29-31)} => \bibelindex {Mat} {15,29-31} \\\
obreakword {(Matthäus15,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([\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\')) $ c>在RegExpEditor中给出了(因为我需要一个Windows免费软件)消息:无效的开始偏移和。* {((\ d +)(?: \.\s +))? (\ w {3})。*?([a-z0-9。, - ] +)} 给出索引1附近无效的重复。



使用免费软件记事本+ +的Windows结果:查找:无效的正则表达式

知道一个在Windows下工作的免费软件,或者是一个正则表达式,它可以和普通的程序一起使用正则表达式搜索和替换函数吗?

解决方案

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



编辑更新正则表达式取决于您的编辑1,2和3。



注意:如果您正在测试多行,则可以增加最大执行时间在regex101上,通过改变右上角的设置。



请参阅 DEMO


For a chronological index (https://tex.stackexchange.com/questions/239006/index-in-chronological-order-with-a-predefined-non-alphabetical-index-list/239200#239200) I'd like to replace following entries (and many more of these types in my document), so that the (number, if present, plus) the first three alpha-letters is written in the first bracket. The second bracket has to start with a number,number and can be followed by something else. Examples:

\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.}

Is it possible to use a single search pattern + single replace pattern with regular expression to replace all of the examples in one step?

EDIT

EDIT 1: I forgot to mention that I have other text which should not be changed:

\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}

EDIT 2: I forgot to mention, that I have also ä/ö/ü characters inside the brackets:

\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)}

EDIT 3: Is it also possible to include patterns in which the numerical part is written on the next line (because of copy paste) there is an ENTER (space/many blanck characters) between the text and numbers, e.g.:

\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}

EDIT 4:

Event though the regex-page perfectly works, https://regex101.com/r/cP1kT6/2 unfortunately has a 2 second limit, which is not enough for me because I've a text with 3000 lines and approximative 200 entries to replace.

The .*!(\d?)(.\s)?(\w{3})\w*\s([\d\w.\-,]*)} towards \\bibelindex{$2$3}{$4} gives in RegExpEditor (because I need a Windows freeware) the message: "Invalid start offset" and .*{((\d+)(?:\.\s+))?(\w{3}).*?([a-z0-9.,-]+)} gives "Invalid repetition near index 1".

Using the freeware "Notepad++" for Windows results in: "Find: Invalid regular expression"

Do you know a freeware which works under Windows, or a regular expression which works with usual programms with regular expression search and replace function?

解决方案

You can use \\index\[bibel\]{((\d+)(?:\.\s+))?(.{3})[\s\S]*?([a-z0-9.,-]+)} to find the matches.

And substitute the matches with \\bibelindex{$2$3}{$4}

Edit: Updated regex depending on your Edits 1,2 and 3.

Note: If you are testing with many lines, you can increase the max execution time on regex101 by changing the settings on top right corner.

See DEMO

这篇关于用正则表达式查找并替换特定的参考模式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆