[Java] Match exact characters in a String
Posted: Thu May 21, 2009 12:07 am
For example,
t should match "water" because the character 't' is in "water"
ret should match "water" because the characters 'r''e''t' are in "water"
retaw should match "water" because the characters 'r''e''t''a''w' are in "water"
BUT
i should NOT match "water" because 'i' character is NOT in the string "water"
watzr should NOT match "water" because 'z' character is NOT in the string "water"
In the following example, i use [rfjl] as a regex, it gives true, when it should give false because the characters 'f''j''l' are NOT in the string "water"
Hope you can help me, thanks in advance !!
t should match "water" because the character 't' is in "water"
ret should match "water" because the characters 'r''e''t' are in "water"
retaw should match "water" because the characters 'r''e''t''a''w' are in "water"
BUT
i should NOT match "water" because 'i' character is NOT in the string "water"
watzr should NOT match "water" because 'z' character is NOT in the string "water"
In the following example, i use [rfjl] as a regex, it gives true, when it should give false because the characters 'f''j''l' are NOT in the string "water"
Code: Select all
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Testing2 {
public static void main(String[] args) {
Pattern p = Pattern.compile("[rfjl]");
String text = "water";
Matcher m = p.matcher(text);
if (m.find()) {
System.out.print("Found !");
}
}
}