Page 1 of 1

[Java] Match exact characters in a String

Posted: Thu May 21, 2009 12:07 am
by username
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"

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 !");
        }
    }
}
 
Hope you can help me, thanks in advance !!

Re: [Java] Match exact characters in a String

Posted: Thu May 21, 2009 2:09 am
by prometheuzz
username wrote: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"

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 !");
        }
    }
}
 
Hope you can help me, thanks in advance !!
Matcher's find() method will return true if there's only one character present in the target String (the 'r' is in 'water'). You should use String's matches(...) method instead:

Code: Select all

System.out.println("water".matches("[retaw]+"));
System.out.println("water".matches("[rfjl]+"));

Re: [Java] Match exact characters in a String

Posted: Thu May 21, 2009 7:19 am
by username
hey many thanks for your answer ! but it doesn't work quite well ...

Code: Select all

 
public class Testing2 {
 
    public static void main(String[] args) {
        String text = "wwwwaaaaaattttttteeeeeerrrrrr";
        if (text.matches("[water]+")) {
            System.out.print("Found !");
        }
    }
}
 
the above code should NOT print "Found!"

Code: Select all

 
public class Testing2 {
 
    public static void main(String[] args) {
        String text = "water";
        if (text.matches("[retawoienpioergnweoigw]+")) {
            System.out.print("Found !");
        }
    }
}
 
thats, gives a possitive find too !!, and it should NOT ! :(

thanks anyway

Re: [Java] Match exact characters in a String

Posted: Fri May 22, 2009 7:22 am
by prometheuzz
username wrote:hey many thanks for your answer ! but it doesn't work quite well ...
...
Ah, it seems you didn't explain your original problem/question properly. Now that you did, I can tell you regex is not suited for this task. You should simply create a custom method that handles this validation.