Joe
Joe

Reputation: 4532

Java Reg ex to check the given value length

I am using the following reg ex to validate whether the given value's length is more than zero

Regex :   .*\S.*; 

value : test

and this the method i use to test :

public boolean isValidInput(String pattern, String value) {
        boolean isValid = false;
        Pattern walletInputPattern = Pattern.compile(pattern);
        Matcher walletMatcher = walletInputPattern.matcher(value);
        if (walletMatcher.matches()) {
            isValid = true;
        }
        return isValid;
    }

but it returns false

desired output
  1. check the given value whether it is not empty and at least has one value.

Thanks

Upvotes: 0

Views: 80

Answers (2)

Joe
Joe

Reputation: 4532

I have used Regex because I don't only check for not empty or length is greater than zero with string I have so many other which uses the same method I provided.

the answer for above question is : ^(?=\\s*\\S).*$

Upvotes: 0

Prabhakaran Ramaswamy
Prabhakaran Ramaswamy

Reputation: 26094

Why not this simple approach?

public boolean isValidInput(String pattern, String value) {
    return value != null && value.trim().length() > 0;
}

Upvotes: 1

Related Questions