无效的转义序列\d

编程入门 行业动态 更新时间:2024-10-14 06:17:04
本文介绍了无效的转义序列\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

本文发布于:2023-10-08 15:07:12,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1472984.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:序列

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!