Reputation: 337
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);
}
Upvotes: 2
Views: 1684
Reputation: 30825
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*
Upvotes: 0
Reputation: 75242
\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+"
Upvotes: 0
Reputation: 10177
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
Upvotes: 2
Reputation: 26930
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]");
Upvotes: 0
Reputation: 12586
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.
Upvotes: 2
Reputation: 5786
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.
Upvotes: 0