MightyOak
MightyOak

Reputation: 23

Java String.matches giving wrong result

I am using String.matches to search a pattern which is present in my input string, but I am getting wrong result. Below is my code.

public class Main {

    public static void main(String[] args) {
        String text =
                "WHERE  ( d.day_key = fact.day_key  \n" +
                    "AND d.full_date BETWEEN  '2013-10-01' AND '2013-12-05' \n" +
                    "AND advac.account_key = fact.advertiser_account_key  \n" +
                    "AND cam.campaign_key = fact.campaign_key  \n" +
                    "AND advac.account_name = 'abc.com') \n";

System.out.println(text.matches("(.*)full_date(.*)"));

    }

}

The above code prints false. Is there anything wrong with my regex? Please advice.

Upvotes: 1

Views: 292

Answers (2)

Keppil
Keppil

Reputation: 46219

You need to enable dotall mode if you want . to also match new line characters. This can for example be done like this:

text.matches("(?s)(.*)full_date(.*)");

You can read more in the JavaDocs.

Upvotes: 2

Raffaele
Raffaele

Reputation: 20885

The problem is that your input contains newlines, so you need to pass the right flag in the regex otherwise the dot metacharacter won't match:

text.matches("(?s)(.*)full_date(.*)")

Upvotes: 1

Related Questions