正则表达式用于排除特殊字符 [英] Regular expression for excluding special characters

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

问题描述

我无法想出一个正则表达式,它基本上会将某些特殊字符列入黑名单。

I am having trouble coming up with a regular expression which would essentially black list certain special characters.

我需要使用它来验证输入字段中的数据(在一个Java Web应用程序)。我们希望允许用户输入任何数字,字母(我们需要包括重音字符,例如法语或德语)和一些特殊字符,例如' - 。等等。

I need to use this to validate data in input fields (in a Java Web app). We want to allow users to enter any digit, letter (we need to include accented characters, ex. french or german) and some special characters such as '-. etc.

如何将<>%$等字符列入黑名单?

How do I blacklist characters such as <>%$ etc?

任何帮助都将是非常感激。

Any help would be greatly appreciated.

推荐答案

我只是将字符列入白名单。

I would just white list the characters.

^[a-zA-Z0-9äöüÄÖÜ]*$

建立一个黑名单与正则表达式同样简单,但你可能需要添加更多的字符 - 在unicode中有很多中文符号...;)

Building a black list is equally simple with regex but you might need to add much more characters - there are a lot of Chinese symbols in unicode ... ;)

^[^<>%$]*$

表达式[^ (这里有很多字符)]只匹配未列出的任何字符。

The expression [^(many characters here)] just matches any character that is not listed.

这篇关于正则表达式用于排除特殊字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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