简体   繁体   中英

Add optional special characters in the regex

I want to add special characters on optional input in regex. currently i am using this for alphanumeric pattern.

 (?!^[0-9]*$)(?!^[a-zA-Z]*$)^([a-zA-Z0-9]{8,20})$

I am using this

 (?!^[0-9]*$)(?!^[a-zA-Z]*$)^([a-zA-Z0-9!@#$&()\\-`.+,\]{8,20})$

for adding special characters but now it gets optional for numeric numbers also, but i want to have a condition of at least one numeric number. Can anyone help me out to set this ?

You can add another lookahead assertion for number presence:

^(?![0-9]+$)(?![a-zA-Z]+$)(?=[^a-zA-Z]*[a-zA-Z])(?=[^0-9]*[0-9])[-a-zA-Z0-9!@#$&()\\`.+,]{8,20}$

Loolahead (?=[^0-9]*[0-9]) will assert presence of at least one digit in input.

RegEx Demo

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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