如何解析一些 Wiki 标记
嘿伙计们,给定一个纯文本数据集,如下所示:
==Events==
* [[312]] – [[Constantine the Great]] is said to have received his famous [[Battle of Milvian Bridge#Vision of Constantine|Vision of the Cross]].
* [[710]] – [[Saracen]] invasion of [[Sardinia]].
* [[939]] – [[Edmund I of England|Edmund I]] succeeds [[Athelstan of England|Athelstan]] as [[King of England]].
*[[1275]] – Traditional founding of the city of [[Amsterdam]].
*[[1524]] – [[Italian Wars]]: The French troops lay siege to [[Pavia]].
*[[1553]] – Condemned as a [[Heresy|heretic]], [[Michael Servetus]] is [[burned at the stake]] just outside [[Geneva]].
*[[1644]] – [[Second Battle of Newbury]] in the [[English Civil War]].
*[[1682]] – [[Philadelphia]], [[Pennsylvania]] is founded.
我想最终得到一个 NSDictionary
或其他形式的集合,以便我可以获得年份(左侧的数字)映射到摘录(右侧的文本)。所以这就是“模板”的样子:
*[[YEAR]] – THE_TEXT
虽然我希望摘录是纯文本,也就是说,没有 wiki 标记,所以没有 [[
集。实际上,对于诸如 [[Edmund I of England|Edmund I]]
之类的别名链接来说,这可能会很困难。
我对正则表达式不太有经验,所以我有几个问题。我应该首先尝试“美化”数据吗?例如,删除始终为 ==Events==
的第一行,并删除 [[
和 ]]
出现的地方?
或者也许是更好的解决方案:我应该分阶段执行此操作吗?例如,第一遍我可以将每一行分成 * [[710]]
和 [[Saracen]] Invasion of [[Sardinia]]
。并将它们存储到不同的 NSArray 中。
然后遍历第一个 NSArray
年份,只获取 [[]]
中的文本(我说文本而不是数字,因为它可以是 530 BC),因此 * [[710]]
变为 710
。
然后,对于摘录 NSArray
,遍历,如果找到 [[some_article|alias]]
,则仅将其设置为 [[alias]] 以某种方式,然后删除所有
[[
和 ]]
集?
这可能吗?我应该使用正则表达式吗?您对正则表达式有什么可能有帮助的想法吗?
谢谢!我真的很感激。
编辑:抱歉造成混乱,但我只想解析上述数据。假设这是我将遇到的唯一类型的标记。我不一定期待解析一般的 wiki 标记,除非已经有一个预先存在的库可以做到这一点。再次感谢!
Hey guys, given a data set in plain text such as the following:
==Events==
* [[312]] – [[Constantine the Great]] is said to have received his famous [[Battle of Milvian Bridge#Vision of Constantine|Vision of the Cross]].
* [[710]] – [[Saracen]] invasion of [[Sardinia]].
* [[939]] – [[Edmund I of England|Edmund I]] succeeds [[Athelstan of England|Athelstan]] as [[King of England]].
*[[1275]] – Traditional founding of the city of [[Amsterdam]].
*[[1524]] – [[Italian Wars]]: The French troops lay siege to [[Pavia]].
*[[1553]] – Condemned as a [[Heresy|heretic]], [[Michael Servetus]] is [[burned at the stake]] just outside [[Geneva]].
*[[1644]] – [[Second Battle of Newbury]] in the [[English Civil War]].
*[[1682]] – [[Philadelphia]], [[Pennsylvania]] is founded.
I would like to end up with an NSDictionary
or other form of collection so that I can have the year (The Number on the left) mapping to the excerpt (The text on the right). So this is what the 'template' is like:
*[[YEAR]] – THE_TEXT
Though I would like the excerpt to be plain text, that is, no wiki markup so no [[
sets. Actually, this could prove difficult with alias links such as [[Edmund I of England|Edmund I]]
.
I am not all that experienced with regular expressions so I have a few questions. Should I first try to 'beautify' the data? For example, removing the first line which will always be ==Events==
, and removing the [[
and ]]
occurrences?
Or perhaps a better solution: Should I do this in passes? So for example, the first pass I can separate each line into * [[710]]
and [[Saracen]] invasion of [[Sardinia]]
. and store them into different NSArrays
.
Then go through the first NSArray
of years and only get the text within the [[]]
(I say text and not number because it can be 530 BC), so * [[710]]
becomes 710
.
And then for the excerpt NSArray
, go through and if an [[some_article|alias]]
is found, make it only be [[alias]]
somehow, and then remove all of the [[
and ]]
sets?
Is this possible? Should I use regular expressions? Are there any ideas you can come up with for regular expressions that might help?
Thanks! I really appreciate it.
EDIT: Sorry for the confusion, but I only want to parse the above data. Assume that that's the only type of markup that I will encounter. I'm not necessarily looking forward to parsing wiki markup in general, unless there is already a pre-existing library which does this. Thanks again!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
此代码假设您正在使用 RegexKitLite:
请注意,您确实需要研究 RegEx 才能很好地构建这些,但这就是我所说的:
忽略大小写,我可以忽略它,因为我没有匹配字母。
?: 表示不捕获此块,我转义 * 来匹配它,然后有零个或多个空格(“ *”),然后我转义两个括号(因为括号也是正则表达式中的特殊字符)。
抓住任何有数字的东西。
这里我们再次忽略一些东西,基本上匹配“-”。注意正则表达式中的任何“\”,我必须在上面的 Objective-C 字符串中添加另一个“\”,因为“\”是字符串中的特殊字符...是的,这意味着匹配正则表达式转义的单个“\”结尾在 Obj-C 字符串中表示为“\\”。
只要抓住其他任何东西,默认情况下,RegEX 引擎将在行尾停止匹配,这就是为什么它不只匹配其他所有内容。您必须添加代码才能从文本中删除 [[LINK]] 内容。
NSRange 变量用于保持整个文件的匹配,而无需重新匹配原始匹配项。这么说吧。
不要忘记添加 RegExKitLite 类文件后,您还需要添加特殊的链接器标志,否则您将收到大量链接错误(RegexKitLite 站点有安装说明)。
This code assumes you are using RegexKitLite:
Note that you really need to study up on RegEx's to build these well, but here's what the one I have is saying:
Ignore case, I could have left that out since I'm not matching letters.
?: means don't capture this block, I escape * to match it, then there are zero or more spaces (" *") then I escape out two brackets (since brackets are also special characters in a regex).
Grab anything that is a number.
Here's where we ignore stuff again, basically matching " – ". Note any "\" in the regex, I have to add another one to in the Objective-C string above since "\" is a special character in a string... and yes that means matching a regex escaped single "\" ends up as "\\" in an Obj-C string.
Just grab anything else, by default the RegEX engine will stop matching at the end of a line which is why it doesn't just match everything else. You'll have to add code to strip out the [[LINK]] stuff from the text.
The NSRange variables are used to keep matching through the file without re-matching original matches. So to speak.
Don't forget after you add the RegExKitLite class files, you also need to add the special linker flag or you'll get lots of link errors (the RegexKitLite site has installation instructions).
我不擅长正则表达式,但这听起来像是他们的工作。我想正则表达式可以很容易地为你解决这个问题。
查看 RegexKitLite 库。
I'm no good with regular expressions, but this sounds like a job for them. I imagine a regex would sort this out for you quite easily.
Have a look at the RegexKitLite library.
如果您希望能够解析一般的维基文本,您有很多工作要做。只有一个复杂的因素是模板。你愿意付出多少努力来应对这些?
如果您对此很认真,您可能应该寻找一个解析维基文本的现有库。简单浏览一下,发现这个 CPAN 库,但我没有使用过它,所以我不能引用它作为个人推荐。
或者,您可能想要采取更简单的方法并决定要处理维基文本的哪些特定部分。例如,这可能是链接和标题,但不是列表。然后你必须专注于其中的每一个,并将维基文本变成你想要的样子。是的,正则表达式在这方面会有很大帮助,所以请仔细阅读它们,如果您有具体问题,请回来询问。
祝你好运!
If you want to be able to parse Wikitext in general, you have a lot of work to do. Just one complicating factor is templates. How much effort do you want to go to cope with these?
If you're serious about this, you probably should be looking for an existing library which parses Wikitext. A brief look round finds this CPAN library, but I have not used it, so I can't cite it as a personal recommendation.
Alternatively, you might want to take a simpler approach and decide which particular parts of Wikitext you're going to cope with. This might be, for example, links and headings, but not lists. Then you have to focus on each of these and turn the Wikitext into whatever you want that to look like. Yes, regular expressions will help a lot with this bit, so read up on them, and if you have specific problems, come back and ask.
Good luck!