每当我在Android Studio上构建项目时,我都会遇到此错误:
非静态方法匹配器(CharSequence)不能从静态上下文中引用
错误发生在第9行:matcher = Pattern.matcher(email);
这是我的代码:
//Email Validation using Regex
public boolean emailChecker(String email){
Pattern pattern;
Matcher matcher;
final String EMAIL_PATTERN = "^[_A-Za-z0-9-]+(\\.[_A-Za-z0-9-]+)*@[A-Za-z0-9]+(\\.[A-Za-z0-9]+)*(\\.[A-Za-z]{2,})$";
pattern = Pattern.compile(EMAIL_PATTERN);
matcher = Pattern.matcher(email);
return matcher.matches();
}
您已经使用了Pattern.matcher这是类名
使用:
pattern.matcher(yourinput); //pattern is your compiled pattern variable
另外我会建议android中的内置模式,以避免任何正则表达式错误
public final static boolean isValidEmail(CharSequence target) {
return (!TextUtils.isEmpty(target) && Patterns.EMAIL_ADDRESS.matcher(target).matches())
}
使用您创建的模式对象
matcher = pattern.matcher(email);