我正在编写一个Perl程序,它需要解析用Wiki标记语言编写的表。表语法使用竖线字符'|‘来分隔列。
| row 1 cell 1 |row 1 cell 2 | row 1 cell 3|
| row 2 cell 1 | row 2 cell 2 |row 2 cell 3|单元格可以包含零个或多个超链接,其语法如下图所示:
[[wiki:path:to:page|Page Title]] or
[[wiki:path:to:page]]请注意,超链接可能包含竖线字符。然而,在这里,它是由[..]括号。
超链接语法不能是嵌套的。
为了匹配和捕获这些表行中的每一个中的第一个单元格,
| Potatoes [[path:to:potatoes]] | Daisies |
| Kiki fruit [[path:to:kiwi|Kiwi Fruit]] | Lemons|我试过了:
qr{\| # match literal pipe
(.*? # non-greedy zero or more chars
(?:\[\[.*?\]\]) # a hyperlink
.*?) # non-greedy zero or more chars
\|}x # match terminating pipe它起作用了,$1包含单元格的内容。
然后,为了匹配
| Potatoes | Daisies |我尝试将超链接设置为可选:
qr{\| # match literal pipe
(.*? # non-greedy zero or more chars
(?:\[\[.*?\]\])? # <-- OPTIONAL hyperlink
.*?) # non-greedy zero or more chars
\|}x # match terminating pipe这是可行的,但在解析时
| Kiki fruit [[path:to:kiwi|Kiwi Fruit]] | Lemons|我只有
Kiki fruit [[path:to:kiwi因此,很明显,如果有选择,它决定忽略超链接模式,并将嵌入的管道视为列分隔符。
我被卡住了。而且我仍然没有处理超链接在一个单元格中多次出现的可能性,也没有考虑将尾随管道返回为下一次迭代中的前导管道。
在Perl的split函数中不一定要使用regexp --如果更容易的话,我可以自己编写拆分循环。我看到许多类似的问题被问到,但似乎没有一个足够密切地处理这个问题。
https://stackoverflow.com/questions/44296613
复制相似问题