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)) {toif (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
PatternandMatcherclasses if you want more advanced regex in the future.See the javadoc on the matches(String) method: