简体   繁体   English

正则表达式的无效转义序列无效

[英]Invalid escape sequence for Regular Expression

Invalid escape sequence (valid ones are \\b \\t \\n \\f \\r \\" \\' \\ ) 无效的转义序列(有效的是\\ b \\ t \\ n \\ f \\ r \\“ \\'\\)

public static final boolean isValidPhoneNumber(CharSequence target) {

    if (target == null || TextUtils.isEmpty(target)) {
        Pattern numberPattern = Pattern.compile("^((\+){0,1}91(\s){0,1}(\-){0,1}(\s){0,1})?([0-9]{10})$");
        Matcher numberMatcher = numberPattern.matcher(target);
        return numberMatcher.matches();
    }

    return false;
}

I used a regular expression checked online was working fine but not working on my android application. 我使用一个在线检查的正则表达式可以正常工作,但不能在我的android应用程序上正常工作。 Plz help... 请帮助...

Your backslashes need to be escaped -- 您的反斜线需要转义-

Pattern numberPattern = Pattern.compile("^((\\+){0,1}91(\\s){0,1}(\\-){0,1}(\\s){0,1})?([0-9]{10})$");

this is because Java using the \\ character as an escape character, to tell it you really mean \\ and not an escape character, you have to write \\\\ . 这是因为Java使用\\字符作为转义字符,要告诉它您的意思是\\而不是转义字符,则必须编写\\\\

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM