Reputation: 2884
I want to regex match text in Wikipedia article source code with following rules:
[[Without|namespace]]
[[Category:Nope]]
, [[File:Nopeish]]
etc.[[Something|else]]
[[text]]
[[Something|else]]
will be matched into two groups with text:
"Something"
"else"
I have tested this and so far I've come up with following regex: \[\[(?!.+?:)(.+?)\|(.+?)\]\]
which is not working as expected since it also matches text like this:
[[Problem]] non link text [[Another link|problemAgain]]
^------------ group 1 (wrong) -------^ ^-group 2 -^
[[This should be|matched|]]
Thanks
Upvotes: 3
Views: 2099
Reputation: 174736
Just use a negated character class instead of .+?
,
\[\[(?!.+?:)([^\]\[]+)\|([^\]\[]+)\]\]
Java regex would be,
"\\[\\[(?!.+?:)([^\\]\\[]+)\\|([^\\]\\[]+)\\]\\]"
OR
simply you could do like this,
\[\[([^\]\[:]+)\|([^\]\[:]+)\]\]
Java regex would be,
"\\[\\[([^\\]\\[:]+)\\|([^\\]\\[:]+)\\]\\]"
Upvotes: 3