2

I am having issues with regex always returning false even though "http://developer.android.com/reference/java/util/regex/Pattern.html" states it shouldn't.

I am entering all kinds of specials charaters "&$@R@," and b/b2 are both returning false in the logcat. The string I am putting into the edit text even displays in logcat as the exact one I input. Anyone have an idea as to why it won't match the alphanumeric characters?

Code:

    EditText et1 = (EditText) findViewById(R.id.editText1);
    String et1Text = et1.getText().toString();
    int et1Length = et1.getText().toString().length();
    EditText et2 = (EditText) findViewById(R.id.editText2);
    String et2Text = et2.getText().toString();
    int et2Length = et2.getText().toString().length();

    Pattern p = Pattern.compile("\\W");
    Log.d(TAG,et1Text);
    Matcher m = p.matcher(et1Text);
    boolean b = m.matches();
    if (b == true){
        Log.d(TAG,"True");
    }
    else {
        Log.d(TAG,"False");
    }
    Log.d(TAG,et2Text);
    Matcher m2 = p.matcher(et2Text);
    boolean b2 = m2.matches();
    if (b2 == true){
        Log.d(TAG,"True");
    }
    else {
        Log.d(TAG,"False");
    }

    if (et1Length < 4 | et1Length > 15 | et2Length < 4 | et2Length > 15){
        Log.d(TAG,"Length dialog");
        dialog(1);
    }

    if (b==true | b2==true){
        Log.d(TAG,"Special char dialog");
        dialog(1);
    }

6 Answers 6

2

Instead of matches() which tries to match the whole string to pattern you can use find() which just tries to find any occurrence of the pattern. Source

Sign up to request clarification or add additional context in comments.

1 Comment

First thing I tried that worked. Thank you. If anyone has any insight on to why I shouldn't use this syntax let me know. Thanks.
2

First of all an uppercase W matches any non-alphanumeric character. Secondly you only matches one single character. To match that the string only contains alphanumerical characters use the following regex:

String pattern = "[\\w]*";

Note that \w also matches underscore.

3 Comments

Where would I add that in the above code? It's a String and I'm reading patters need to be Pattern. Also yes, I am trying to make sure the whole string is alphanumeric only.
Where you construct your pattern you assign it a string: Pattern p = Pattern.compile("\\W");. Just replace the "\\W" with the above mentioned string :)
This example you give is actually the exact opposite of what I was needing. Not in a bad way it will work if I (b==false |b2==false). It went ahead and went with the find(). Thanks for your help.
0

The Matcher method, "matches", tries to match the Pattern against the entire region. The regular expression "\W" matches a SINGLE non-alphanumeric character. In other words, m.matches() will return true when you attempt to match it to a SINGLE special character. If you attempt to match it to "&$@R@,", it will return false since the string contains more than just a single non-alphanumeric character.

Comments

0

I have no idea about android but this regex :

Pattern p = Pattern.compile("\\W");

Will match any non alphanumeric character. If you would expand it, it would look like this :

[^a-zA-Z0-9_]

If you want to match a single alphanumeric character including _ just use :

Pattern p = Pattern.compile("\\w");

else use : Pattern p = Pattern.compile("[a-zA-Z0-9]");

Comments

0

\W (or as a java string literal, "\\W") matches one non-word character. The matches() method implicitly anchors the match at both ends, as if you had really written "\\A\\W\\z". So you'll only get a match if the string consists of exactly one non-word character. If you want to match one or more characters, you should change the regex to

"\\W+"

Comments

0

If you want a regular express that matches all alphanumeric characters try [a-zA-Z0-9]*. If you want to match all non alphanumeric characters in a string try this \\W*

1 Comment

The square brackets in your second regex are redundant, and at least one of those backslashes has to go. In "raw" form the regex would be \W*, and as a Java string literal, "\\W*". (But I think + is more appropriate than *.)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.