Pavel
Pavel

Reputation: 4420

How to include symbol of the end of the line ("\n") in regexp

i have regular expression which need to cover multi lines(delete comments from pascal file)

\(\*.*?\*\)|\{.*?\}|\/\/(.*$)

this works almost fine but

\(\*.*?\*\)

and

\{.*?\}

are supposed to work for multilines, but work for single only. How to make them working right(and dont make

//(.*$)

work for multi lines)

Thanks in advance

Upvotes: 0

Views: 89

Answers (2)

FailedDev
FailedDev

Reputation: 26940

This will match literally everything, including newlines:

Pattern regex = Pattern.compile("[\\s\\S]*");

Upvotes: 1

Qtax
Qtax

Reputation: 33928

You are looking for the Pattern.DOTALL flag. Pass it to Pattern.compile like so:

Pattern p = Pattern.compile("regex", Pattern.DOTALL); 

You can also set it in the regex with (?s), like: "(?s)regex"

Upvotes: 1

Related Questions