SoulRayder
SoulRayder

Reputation: 5166

Regex pattern to check if anything other than space character exists

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

String s = "[^ \s]" 

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

Upvotes: 2

Views: 1549

Answers (5)

user3064914
user3064914

Reputation: 959

Try this regex pattern

pattern= "(\S)+"

Upvotes: 0

Jay kumar
Jay kumar

Reputation: 277

this could help you

String regex= "\\S"

Upvotes: 0

anubhava
anubhava

Reputation: 785196

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

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.

Upvotes: 0

Masudul
Masudul

Reputation: 21961

Use capital S.

  String regex= "\\S"

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

Upvotes: 2

Bohemian
Bohemian

Reputation: 425043

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

.*\S.*

In 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*"))

Upvotes: 1

Related Questions