正则表达式包括和排除特殊字符 [英] Regular expression include and exclude special characters
问题描述
我找到一个符合规则的正则表达式。
I'm finding a regular expression which adheres below rules.
允许的字符
字母:az / AZ
数字:0-9 < br>
特殊字符:〜@#$ ^& *() - _ + = [] {} | \,。 ? :
(应允许空格)
Alphabet : a-z / A-Z
Numbers : 0-9
Special Characters : ~ @ # $ ^ & * ( ) - _ + = [ ] { } | \ , . ? :
(spaces should be allowed)
不允许
特殊字符:< ; >'/;`%
Special Characters : < > ' " / ; ` %
推荐答案
对于允许使用的字符,您可以使用
For the allowed characters you can use
^[a-zA-Z0-9~@#$^*()_+=[\]{}|\\,.?: -]*$
验证一个只包含允许字符的完整字符串。注意 -
在最后(因为否则它是一个范围)并且转义了几个字符。
to validate a complete string that should consist of only allowed characters. Note that -
is at the end (because otherwise it'd be a range) and a few characters are escaped.
对于无效字符你可以使用
For the invalid characters you can use
[<>'"/;`%]
检查它们。
要将两者合并为一个正则表达式,您可以使用
To combine both into a single regex you can use
^(?=[a-zA-Z0-9~@#$^*()_+=[\]{}|\\,.?: -]*$)(?!.*[<>'"/;`%])
但你需要一个允许前瞻的正则表达式引擎。
but you'd need a regex engine that allows lookahead.
这篇关于正则表达式包括和排除特殊字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!