Home > front end >  How to fix java.util.regex.PatternSyntaxException in android
How to fix java.util.regex.PatternSyntaxException in android

Time:03-16

public static boolean isEmailValid(String email) {
        boolean isValid = false;

        String expression = "^[\\w\\.-] @([\\w\\-] \\.) [A-Z]{2,4}$";

        Pattern pattern = Pattern.compile(expression, Pattern.CASE_INSENSITIVE);
        Matcher matcher = pattern.matcher(email);
        if (matcher.matches()) {
            isValid = true;
        }
        return isValid;
    }

I am calling this in Activity for email validation

  if (!Util.isEmailValid(binding.etEmail.getText().toString().trim())) {
                Toast.makeText(this, getResources().getString(R.string.valid_email), Toast.LENGTH_SHORT).show();
                return false;
            }

i am getting the crash report .

java.util.regex.PatternSyntaxException: Error in {min,max} interval near index 7 ( \d{٣}[\s-]?\d{0}[\s-]?)

let me know if not clear i will post full crash report

Please help me what i am doing wrong and how to fix this crash .

CodePudding user response:

Use this

String emailPattern = "[a-zA-Z0-9._-] @[a-z] \\. [a-z] "

instead of this

String emailPattern = "^[\\w\\.-] @([\\w\\-] \\.) [A-Z]{2,4}$"
  • Related