即使“http://developer.android.com/reference/java/util/regex/Pattern.html”声明它不应该,我也遇到正则表达式总是返回错误的问题。
我正在输入各种特殊字符“&$@R@”,并且 b/b2 在 logcat 中都返回 false。我放入编辑文本的字符串甚至在 logcat 中显示为我输入的确切字符串。任何人都知道为什么它与字母数字字符不匹配?
代码:
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);
}