Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Wikilinks - turn the text [[a]] into an internal link

I need to implement something similar to wikilinks on my site. The user is entering plain text and will enter [[asdf]] wherever there is an internal link. Only the first five examples are really applicable in the implementation I need.

Would you use regex, what expression would do this? Is there a library out there somewhere that already does this in C#?

like image 230
DavGarcia Avatar asked Sep 03 '25 05:09

DavGarcia


1 Answers

On the pure regexp side, the expression would rather be:

\[\[([^\]\|\r\n]+?)\|([^\]\|\r\n]+?)\]\]([^\] ]\S*)
\[\[([^\]\|\r\n]+?)\]\]([^\] ]\S*)

By replacing the (.+?) suggested by David with ([^\]\|\r\n]+?), you ensure to only capture legitimate wiki links texts, without closing square brackets or newline characters.

([^\] ]\S+) at the end ensures the wiki link expression is not followed by a closing square bracket either.

I am note sure if there is C# libraries already implementing this kind of detection.

However, to make that kind of detection really full-proof with regexp, you should use the pushdown automaton present in the C# regexp engine, as illustrated here.

like image 129
VonC Avatar answered Sep 04 '25 21:09

VonC



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!