문제

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?

도움이 되었습니까?

해결책

By default . won't match newlines:

You need to toggle single line mode

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

With single line mode, . would also match newlines

다른 팁

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]]>
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top