简体   繁体   English

在正则表达式中添加可选的特殊字符

[英]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. Loolahead (?=[^0-9]*[0-9])将断言输入中至少有一位数字。

RegEx Demo 正则演示

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

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