正则表达式:匹配除单词以外的任何非单词和非数字字符 [英] RegEx: match any non-word and non-digit character except

查看:80
本文介绍了正则表达式:匹配除单词以外的任何非单词和非数字字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为匹配任何非单词和非数字字符(特殊字符),我使用以下代码: [\\ W \\ D] .如果我也想忽略一些具体的字符,应该添加什么?比方说下划线.

To match any non-word and non-digit character (special characters) I use this: [\\W\\D]. What should I add if I want to also ignore some concrete characters? Let's say, underscore.

推荐答案

首先,您必须知道 \ W 等效于 [^ a-zA-Z0-9_].因此,您可以将当前的正则表达式更改为:

First of all, you must know that \W is equivalent to [^a-zA-Z0-9_]. So, you can change your current regex to:

[\\W]

这将自动处理 \ D .

现在,如果您想忽略其他字符,例如说& (在 \ W 中已经排除了下划线),则可以使用否定的字符类:

Now, if you want to ignore some other character, say & (underscore is already exluded in \W), you can use negated character class:

[^\\w&]

这篇关于正则表达式:匹配除单词以外的任何非单词和非数字字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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