Reputation: 755
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"
Upvotes: 0
Views: 90
Reputation: 7326
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
Upvotes: 4