Zack
Zack

Reputation: 385

Java regular expression dotall

Until now I used this one:

"(?s).*\\(.*\\).*\\{.*\\}.*\\;.*"

I apply this on full request (so is multiline). The problem is that it matches also when I have new lines between (){}; which I don't want. What I want to match is (){}; with any character inside this expression (except new lines), the new lines should be matched only before and after this expression.

So is there any way to specify only for one specific dot that it should match also new lines?

Upvotes: 1

Views: 1543

Answers (2)

Bohemian
Bohemian

Reputation: 424993

Insert (?-s), which turns DOTALL off, before the dot, then turn it back on again with another (?s):

"(?s).*\\(.*\\).*\\{(?-s).*\\}(?s).*\\;.*"

Or much better, temporarily turn it off just for the expression (thanks to @nhahtdh for this great suggestion!):

"(?s).*\\(.*\\).*\\{(?-s:.*)\\}.*\\;.*"

Upvotes: 4

anubhava
anubhava

Reputation: 785058

You can just remove ?s:

".*\\(.*\\).*\\{.*\\}.*\\;.*"

Or else use negation:

"(?s).*\\([^\\n]*\\).*\\{[^\\n]*\\}.*\\;.*"

Upvotes: 2

Related Questions