Regex模式检查是否存在除空格字符之外的任何内容

时间:2023-02-01 17:33:32

I am trying to create a pattern matcher for anything other than space, that is it should return true if I find anything other than space character, in the entry. I have tried using

我正在尝试为除空间之外的任何东西创建一个模式匹配器,即如果我在条目中发现除了空间字符以外的任何东西,它应该返回true。我已经尝试使用

String s = "[^ \s]" 

as the pattern matcher but it complains as error. What is the correct string to use to generate the pattern matcher?

作为模式匹配程序,但它报错。用于生成模式匹配器的正确字符串是什么?

5 个解决方案

#1


2  

Use capital S.

用大写。

  String regex= "\\S"

Note: Your code complain error because you should use double \\ inside String.

注意:您的代码抱怨错误,因为您应该使用双\内部字符串。

#2


1  

Look for at least one non-space anywhere in the input:

在输入中至少找一个非空格:

.*\S.*

In java:

在java中:

if (input.matches(".*\\S.*"))
    // there is at least one non-space character

Or just negate the (simpler) test for all spaces:

或者干脆否定所有空间的(更简单的)测试:

if (!input.matches("\\s*"))

#3


0  

it should return true if I find anything other than space character, in the entry.

如果我在条目中找到除空格字符以外的任何字符,它应该返回true。

You don't need any regex for this. You can use:

你不需要任何正则表达式。您可以使用:

if (!string.replace(" ", "").isEmpty()) {...}

This will return true if there is any non-space character in the string.

如果字符串中有任何非空格字符,则返回true。

#4


0  

this could help you

这可以帮助你

String regex= "\\S"

#5


0  

Try this regex pattern

试试这个正则表达式模式

pattern= "(\S)+"

#1


2  

Use capital S.

用大写。

  String regex= "\\S"

Note: Your code complain error because you should use double \\ inside String.

注意:您的代码抱怨错误,因为您应该使用双\内部字符串。

#2


1  

Look for at least one non-space anywhere in the input:

在输入中至少找一个非空格:

.*\S.*

In java:

在java中:

if (input.matches(".*\\S.*"))
    // there is at least one non-space character

Or just negate the (simpler) test for all spaces:

或者干脆否定所有空间的(更简单的)测试:

if (!input.matches("\\s*"))

#3


0  

it should return true if I find anything other than space character, in the entry.

如果我在条目中找到除空格字符以外的任何字符,它应该返回true。

You don't need any regex for this. You can use:

你不需要任何正则表达式。您可以使用:

if (!string.replace(" ", "").isEmpty()) {...}

This will return true if there is any non-space character in the string.

如果字符串中有任何非空格字符,则返回true。

#4


0  

this could help you

这可以帮助你

String regex= "\\S"

#5


0  

Try this regex pattern

试试这个正则表达式模式

pattern= "(\S)+"