By default .
won't match newlines:
You need to toggle single line mode
(?s)\\S*\\[\\[.*?\\]\\]\\S*
^
With single line mode, .
would also match newlines
Question
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
OTHER TIPS
You can use this regex with DOTALL
switch (?s)
:
(?s)\\S*\\[\\[.*?\\]\\]\\S*
System.out.println("has [[public transport]]ation in city? some[[times]]".replaceAll(
"(?s)\\S*\\[\\[.*?\\]\\]\\S*", "<$0>"));
OUTPUT:
has <[[public transport]]ation> in city? <some[[times]]>