Find and replace a specific reference pattern by a

2019-08-26 01:58发布

问题:

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?

回答1:

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