Add the following:
- All but
|zero or more:[^|]* - ... and then
|:| - ... additionally:
? - Group it using
(?: ... )if you don't want to record it.
Here is a complete example:
String text1 = "something something [[abcd]] blah blah";
String text2 = "something something [[xyz|abcd]] blah blah";
Pattern pattern = Pattern.compile("\\[\\[(?:[^|]*\\|)?(.+?)\\]\\]");
System.out.println(pattern.matcher(text1).replaceAll("$1"));
System.out.println(pattern.matcher(text2).replaceAll("$1"));
Conclusion:
something something abcd blah blah
something something abcd blah blah
source
share