javaregexwiki-markup

Regex to get a blended link from a wikimarkup page in Java


I have to check for the following kind of string:

has [[public transport]]ation in city? some[[times]]

and return only the relevant parts - i.e.

[[public transport]]ation some[[times]]

I am using this regex: \\w*?\\[\\[(.*?)\\]\\]\\w*?

It does not seem to work with the example given above and extended examples when the search text contains new lines and special characters too. Can you indicate how i should write the regex?


Solution

  • By default . won't match newlines:

    You need to toggle single line mode

    (?s)\\S*\\[\\[.*?\\]\\]\\S*
     ^
    

    With single line mode, . would also match newlines