简体   繁体   English

在现有正则表达式中添加几个特殊字符

[英]Adding several special characters to existing regex

I use a regex pattern in a change password feature of my app. 我在我的应用程序的更改密码功能中使用正则表达式模式。 The requirements for the password are: 密码的要求是:

  • at least 1 small letter 至少1封小写字母
  • at least 1 upper case letter 至少1个大写字母
  • minimum of 8 characters 最少8个字符
  • at least 1 number 至少1个号码
  • at least 1 special character 至少1个特殊字符

I want to add these characters to my pattern, but I couldn't seem to add the "\\" and the single and double quotes. 我想将这些字符添加到我的模式中,但我似乎无法添加“\\”以及单引号和双引号。 How do I escape them? 我怎么逃避他们? I'm using Eclipse (Java) so escaping needs 2 backslashes 我正在使用Eclipse(Java),所以转义需要2个反斜杠

Right now, my pattern looks like this and it's giving me an error: 现在,我的模式看起来像这样,它给了我一个错误:

String customPattern = "((?=.*\\d)(?=.*[a-z])(?=.*[A-Z])
(?=.*[!@#$%^&*()\\-_+=/?.,>~\\\\|\\[];:\\"'{}])(?=[\\S]+$).{8,})";

把一个反斜杠双引号之前,也难逃]目前焦炭类中。

String customPattern = "^(?=.*\\d)(?=.*[a-z])(?=.*[A-Z])(?=.*[!@#$%^&*()\\-_+=/?.,>~\\\\|\\[\\];:\"'{}])(?=\\S+$).{8,}";

You did not escape the ] inside the character class [!@#$%^&*()\\\\-_+=/?.,>~\\\\\\\\|\\\\[];:\\\\"'{}] (right after \\\\[ ). Here is a working regex (with ^ / $ anchors that will really enforce length check of the whole string if not used with matches() ): 你没有逃过]里面的字符类[!@#$%^&*()\\\\-_+=/?.,>~\\\\\\\\|\\\\[];:\\\\"'{}] (在\\\\[ )。这是一个正常工作的正则表达式(如果不与matches()一起使用,将使用^ / $锚点来强制执行整个字符串的长度检查):

^(?=.*\d)(?=.*[a-z])(?=.*[A-Z])(?=.*[!@#$%^&*()\-_+=\/?.,>~\\|\[\];:"'{}])(?=\S+$).{8,}$
                                                                ^^

See demo 演示

In Java, you can use it as 在Java中,您可以将其用作

String customPattern = "^(?=.*\\d)(?=.*[a-z])(?=.*[A-Z])(?=.*[!@#$%^&*()\\-_+=/?.,>~\\\\|\\[\\];:\"'{}])(?=\\S+$).{8,}$";

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

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