简体   繁体   English

Java String.matches给出了错误的结果

[英]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. 我正在使用String.matches来搜索输入字符串中存在的模式,但结果却是错误的。 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. 请指教。

You need to enable dotall mode if you want . 如果需要,您需要启用dotall模式. 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 . 您可以在JavaDocs中阅读更多内容。

问题是您的输入包含换行符,因此您需要在正则表达式中传递右侧标志,否则点元字符将不匹配:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM