Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regular expression for not empty

Tags:

java

regex

I need a Java regular expression, which checks that the given String is not Empty. However the expression should ingnore if the user has accidentally given whitespace in the beginning of the input, but allow whitespaces later on. Also the expression should allow scandinavian letters, Ä,Ö and so on, both lower and uppercase.

I have googled, but nothing seems ro quite fit on my needs. Please help.

like image 396
jaana Avatar asked Dec 15 '10 10:12

jaana


People also ask

Can a regular expression be empty?

∅, the empty set, is a regular expression. ∅ represent the language with no elements {}.

Does empty regex match everything?

An empty regular expression matches everything.

What is whitespace regular expression?

The most common regex character to find whitespaces are \s and \s+ . The difference between these regex characters is that \s represents a single whitespace character while \s+ represents multiple whitespaces in a string.


2 Answers

You can also use positive lookahead assertion to assert that the string has atleast one non-whitespace character:

^(?=\s*\S).*$

In Java you need

"^(?=\\s*\\S).*$"
like image 61
codaddict Avatar answered Sep 24 '22 14:09

codaddict


For a non empty String use .+.

like image 21
Lavish Avatar answered Sep 26 '22 14:09

Lavish