Search code examples
regexmediawiki-api

Regex for new line after last match


I am using a program called AutoWikiBrowser to do some mass editing on a wiki. It is basically a search-and-replace using regular expressions. I want to replace the following text:

{{Infobox Book/List/Header|Second bookcase}}
{{:Book title 1|List}}
{{:Book title 2|List}}
{{:Book title 3|List}}

by the following text:

{{BookList|type=List/Sorted|caption=Second bookcase
 |Book title 1
 |Book title 2
 |Book title 3
}}

I have successfully come up with the following regex to replace the first line, find:

\{\{Infobox Book/List/Header\|(.*?)}}

and replace:

{{BookList|type=List/Sorted|caption=$1

and one or more of the next lines are found with:

\{\{:(.*?)\|List}}

and replaced with:

 |$1

However, after this last search-and-replace has happened, I want to add those two closing curly brackets on a new line. I am having trouble finding a way to implement this, because on forehand I cannot know how much book title rows there will be. Does somebody know a smart trick?


Solution

  • Simply add a new regex to replace the whole book list thing without the brackets to one with brackets:

    {{BookList\|type=List\/Sorted\|caption=[^\n]+\n( \|[^\n]+\n)+
    

    to

    $0}}
    

    Obviously, this needs to be tested and adapted against the numerous Wiki false positive that could occur since the data is rarely consistant there.

    You can see it here on regex101