无效的转义序列\d [英] Invalid escape sequence \d

查看:138
本文介绍了无效的转义序列\d的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试检查密码是否包含至少一个小写字母,一个大写字母,一个数字和一个特殊字符。

I'm trying to check if a password contain at least one lower case letter, one upper case letter, one digit and one special character.

我是试试这个:

if(!password.matches("(?=.*\d)(?=.*[a-z])(?=.*[A-Z])(?=.*[@#$%^&+=])")){
        username = "Error";
    }

但是给我一个错误说:无效的转义序列。

but give me an error saying: invalid escape sequence.

有人可以帮我解决问题,并确认这是正确的模式吗?

Someone can help me to solve the problem and can confirm that is a correct pattern?

谢谢,惠特\\\\不要做错误,但它与保罗%88之类的字符串不匹配为什么?

Thanks, whit \\d don't do error but it don't match with a string like Paul%88 why?

推荐答案

Java会处理 \ 在字符串内作为转义序列。请确保使用 \\ (这样您就可以在字符串中获得实际的 \ 字符)和你应该没事。

Java will treat \ inside a string as starting an escape sequence. Make sure you use \\ instead (so that you get an actual \ character in the string) and you should be ok.

快速更新:正如Etienne指出的那样,如果你真的想要 \ 在RegEx本身,你需要使用 \\\\ ,因为那样会产生 \字符串中的\ ,它将在RegEx中产生 \

Quick Update: As Etienne points out, if you actually want a \ in the RegEx itself, you'll need to use \\\\, since that will produce \\ in the string, which will produce \ in the RegEx.

新问题更新:您提到您的RegEx不起作用,我很确定这是因为它错了。如果您只想确保每种类型的字符类都存在,您可能只想为每个类创建一个RegEx,然后检查每个类的密码。密码几乎可以保证很短(你实际上可以自己控制密码),所以性能指数应该是最小的。

New Question Update: You mention that your RegEx doesn't work, and I'm pretty sure that's because it's wrong. If you just want to ensure one of each type of character class is present, you may just want to create one RegEx for each class, and then check the password against each one. Passwords are pretty much guaranteed to be short (and you can actually control that yourself) so the perf hit should be minimal.

这篇关于无效的转义序列\d的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆