I am trying to parse some wiki markup. For example, the following:
{{Some infobox royalty|testing
| name = Louis
| title = Prince Napoléon
| elevation_imperial_note= <ref name="usgs">{{cite web|url={{Gnis3|1802764}}|title=USGS}}</ref>
| a = [[AA|aa]] | b = {{cite
|title=TITLE
|author=AUTHOR}}
}}
can be the text to start with. I first remove the starting {{ and ending }}, so I can assume those are gone.
I want to do .split(<regex>) on the string to split the string by all | characters that are not within braces or brackets. The regex needs to ignore the | characters in [[AA|aa]], <ref name="usgs">{{cite web|url={{Gnis3|1802764}}|title=USGS}}</ref>, and {{cite|title=TITLE|author=AUTHOR}}. The expected result is:
[
'testing'
'name = Louis',
'title = Prince Napoléon',
'elevation_imperial_note= <ref name="usgs">{{cite web|url={{Gnis3|1802764}}|title=USGS}}</ref>',
'a = [[AA|aa]]',
'b = {{cite\n|title=TITLE\n|author=AUTHOR}}'
]
There can be line breaks at any point, so I can't just look for \n|. If there is extra white space in it, that is fine. I can easily strip out extra \s* or \n*.
The following is a pure-Ruby solution. I assume the braces and brackets in the string are balanced.
Note that, to improve readability, I've broken the string that is the antepenultimate element of the returned array1.
Explanation
For an explanation of how the locations of the pipe symbols on which to split are determined, run the Heredoc above to determine
str(the Heredoc needs to be un-indented first), then run the following code. All will be revealed. (The output is long, so focus on changes to the arrayslocsandstack.)If desired, one can confirm the braces and brackets are balanced as follows.
1 ...and, in the interest of transparency, to have the opportunity to use a certain word.