简体   繁体   English

如何在现有的正则表达式中允许一组特殊字符?

[英]How to allow set of special characters in existing regex?

We have regex to validate password with one digit, one upper case letter and one lower case letter. 我们有正则表达式验证密码,一个数字,一个大写字母和一个小写字母。 Regex is: 正则表达式是:

^\w*(?=\w*\d)(?=\w*[a-z])(?=\w*[A-Z])\w*$

This regex will not allow any special characters. 此正则表达式不允许任何特殊字符。 I need to change regex to allow some list of special characters and there should not be any restriction that there must be at least one special character. 我需要更改正则表达式以允许一些特殊字符列表,并且不应该有任何限制,必须至少有一个特殊字符。 Only [-!$%^&*()_+|~=`{}\\[\\]:";'<>?,.\\/] should be allowed as special characters without must have one restriction. 只允许[-!$%^&*()_+|~=`{}\\[\\]:";'<>?,.\\/]作为特殊字符,不得有一个限制。

I tried: 我试过了:

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

and which seems to be wrong. 这似乎是错的。 Please some one help. 请一些帮助。

This is because of \\w* before $ .You are specifically trying to match 0 to many words..Try this: 这是因为在$之前的\\w* 。你是专门尝试匹配0到多个单词。试试这个:

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

OR 要么

^(?=.*\d)(?=.*[a-z])(?=.*[A-Z])[\w\p{Punct}]+$

\\p{Punct} is a special character class similar to [!"#$%&'()*+,\\-./:;<=>?@[\\\\\\]^_{|}~] \\p{Punct}是一个特殊的字符类,类似于[!"#$%&'()*+,\\-./:;<=>?@[\\\\\\]^_{|}~]

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

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