jbaiter
jbaiter

Reputation: 7099

Java regex: Matching Substring with newline followed by whitespace

I am currently trying to extract Substrings from Strings by matching them with a regular expression. The input-strings are all in the form foo.bar("""foobar"""), where foobar is the substring I would like to extract. This is the regular expression I wrote for this task:

Pattern pattern = Pattern.compile(
            ".+\\(\"{3}(.+)\"{3}\\)" , Pattern.MULTILINE);

It matches well against simple Strings, but fails whenever a newline followed by a whitespace occurs in the String to be matched, i.e. foo.bar("""foo\n bar""")

How do I have to change my pattern so that it matches those strings as well?

Upvotes: 1

Views: 1835

Answers (1)

axtavt
axtavt

Reputation: 242706

You need Pattern.DOTALL instead of Pattern.MULTILINE. Pattern.MULTILINE is about behaviour of ^ and $ and has nothing to do with matching newlines with .:

Pattern pattern = Pattern.compile(
     ".+\\(\"{3}(.+)\"{3}\\)" , Pattern.DOTALL); 

Upvotes: 8

Related Questions