正则表达式包括和排除特殊字符 [英] Regular expression include and exclude special characters

查看:2011
本文介绍了正则表达式包括和排除特殊字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我找到一个符合规则的正则表达式。

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆