I keep running into this error whenever I build projects on Android Studio:
non-static method matcher(CharSequence) cannot be referenced from a static context
The error occurs at line 9: matcher = Pattern.matcher(email);
This is my code:
//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();
}
You've used Pattern.matcher which is the class name
Use:
pattern.matcher(yourinput); //pattern is your compiled pattern variable
Also i would suggest the built in pattern in android to avoid any regex errors
public final static boolean isValidEmail(CharSequence target) {
return (!TextUtils.isEmpty(target) && Patterns.EMAIL_ADDRESS.matcher(target).matches())
}
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments