简体   繁体   中英

regex wont work

maybe someone can help me with a regular expression. This one wont work:

public static void main(String[] args){
    String pattern = "(?i).*a[\\s-\\.]?nton.*";
    String text = "a-nton vom 27.2.2012";
    if (pattern.matches(text)){
        System.out.println("FOUND");
    }else{
        System.out.println("NOT FOUND");
    }
}

The regex should be true if text contains one of these words:

a-nton OR a nton OR anton

before and after this word can be any text

But the pattern above will be "NOT FOUND"

Change if (pattern.matches(text)) { to if (text.matches(pattern)) { .

The matches method checks in the String invoked on for the pattern passes to the method, so invoke it on text and pass it pattern. You may also want to look at the Pattern and Matcher classes if you want more advanced regex in the future.

See the javadoc on the matches(String) method:

Parameters: regex - the regular expression to which this string is to be matched

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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