RegEx: match any non-word and non-digit character except

倖福魔咒の 提交于 2021-02-16 16:13:15

问题


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.


回答1:


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

[\\W]

This will automatically take care of \D.

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

[^\\w&]


来源:https://stackoverflow.com/questions/18244482/regex-match-any-non-word-and-non-digit-character-except

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!