Java正则表达式匹配除

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16207853/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-16 05:48:19  来源:igfitidea点击:

Java regex match all characters except

javaregex

提问by Tommy

What is the correct syntax for matching all characters except specific ones.

匹配除特定字符之外的所有字符的正确语法是什么。

For example I'd like to match everything but letters [A-Z] [a-z]and numbers [0-9].

例如,我想匹配除字母[A-Z] [a-z]和数字之外的所有内容[0-9]

I have

我有

string.matches("[^[A-Z][a-z][0-9]]")

Is this incorrect?

这不正确吗?

采纳答案by Paul

Yes, you don't need nested []like that. Use this instead:

是的,你不需要[]像那样嵌套。改用这个:

"[^A-Za-z0-9]"

It's all one character class.

这都是一个字符类。

回答by Vasyl Keretsman

string.matches("[^A-Za-z0-9]")

回答by Miquel

Almost right. What you want is:

几乎正确。你想要的是:

string.matches("[^A-Za-z0-9]")

Here's a good tutorial

这是一个很好的教程

回答by stema

If you want to match anything but letters, you should have a look into Unicode properties.

如果您想匹配除字母以外的任何内容,您应该查看Unicode properties

\p{L}is any kind of letter from any language

\p{L}是来自任何语言的任何类型的字母

Using an uppercase "P" instead it is the negation, so \P{L}would match anything that is not a letter.

使用大写的“P”代替它是否定,所以\P{L}会匹配任何不是字母的东西。

\dor \p{Nd}is matching digits

\d或者\p{Nd}正在匹配数字

So your expression in modern Unicode style would look like this

所以你在现代 Unicode 风格中的表达看起来像这样

Either using a negated character class

要么使用否定字符类

[^\p{L}\p{Nd}]

or negated properties

或否定属性

[\P{L}\P{Nd}]

The next thing is, matches()matches the expression against the complete string, so your expression is only true with exactly one char in the string. So you would need to add a quantifier:

下一件事是,matches()将表达式与完整字符串进行匹配,因此您的表达式仅在字符串中只有一个字符时才为真。所以你需要添加一个量词:

string.matches("[^\p{L}\p{Nd}]+")

returns true, when the complete string has only non alphanumerics and at least one of them.

返回真,当完整的字符串只有非字母数字并且至少有一个时。

回答by Michael Riess

Lets say that you want to make sure that no Strings have the _ symbol in them, then you would simply use something like this.

假设您想确保字符串中没有 _ 符号,那么您可以简单地使用这样的东西。

    Pattern pattern = Pattern.compile("_");
    Matcher matcher = Pattern.matcher(stringName);
    if(!matcher.find()){
       System.out.println("Valid String");
    }else{
        System.out.println("Invalid String");
     }