[Java] Match exact characters in a String

Any questions involving matching text strings to patterns - the pattern is called a "regular expression."

Moderator: General Moderators

Post Reply
username
Forum Newbie
Posts: 2
Joined: Thu May 21, 2009 12:05 am

[Java] Match exact characters in a String

Post 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 !!
Last edited by Benjamin on Fri May 22, 2009 10:01 am, edited 1 time in total.
Reason: Changed code type from text to java.
User avatar
prometheuzz
Forum Regular
Posts: 779
Joined: Fri Apr 04, 2008 5:51 am

Re: [Java] Match exact characters in a String

Post 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]+"));
username
Forum Newbie
Posts: 2
Joined: Thu May 21, 2009 12:05 am

Re: [Java] Match exact characters in a String

Post 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
Last edited by Benjamin on Fri May 22, 2009 10:02 am, edited 1 time in total.
Reason: Changed code type from text to java.
User avatar
prometheuzz
Forum Regular
Posts: 779
Joined: Fri Apr 04, 2008 5:51 am

Re: [Java] Match exact characters in a String

Post 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.
Post Reply