-1

There is regular expression for finding blank string and I want only negation. I also see this question but it does not work for java (see examples). Solution also not work for me (see 3-rd line in example).

For example

Pattern.compile("/^$|\\s+/").matcher(" ").matches() - false
Pattern.compile("/^$|\\s+/").matcher(" a").matches()- false
Pattern.compile("^(?=\\s*\\S).*$").matcher("\t\n a").matches() - false

return false in both cases.

P.S. If something is not clear ask me questions.

UPDATED

I want to use this regular expression in @Pattern annotation without creating custom annotation and programmatic validator for it. That's why I want a "plain" regexp solution without using find function.

Community
  • 1
  • 1
Cherry
  • 31,309
  • 66
  • 224
  • 364

3 Answers3

5

It's not clear what you mean by negation.

If you mean "a string that contains at least one non-blank character," then you can use this:

Pattern.compile("\\S").matcher(str).find()

If it's really necessary to use matches, then you can do it with this.

Pattern.compile("\\A\\s*\\S.*\\Z").matcher(str).matches()

This just matches 0 or more spaces followed by a non-space followed by any characters at all up to the end of the string.

If you mean "a string that is all non-blank with at least one such character," then you can use this:

Pattern.compile("\\A\\S+\\Z").matcher(str).matches()

You need to study the Java regex syntax. In Java, regular expressions are compiled from strings, so there's no need for special delimiters like /.../ or %r{...} as you'll see in other languages.

Gene
  • 46,253
  • 4
  • 58
  • 96
  • Congratulations to your 10k! – Joachim Sauer Aug 21 '13 at 12:11
  • Pattern.compile("\\S").matcher("abc").matches() - false, but must be true. Your solution is only for 1 character in the string. – Cherry Aug 22 '13 at 02:24
  • 2
    @Cherry Use `find` instead of `match`. I edited the answer to make this explicit. – Gene Aug 22 '13 at 02:34
  • 1
    @Cherry Then what do you mean by negation? The opposite of a string of all spaces is a string that is not all spaces, which is a string that contains at least one non-space. That's what I gave you the regex for... – Gene Aug 22 '13 at 02:42
  • The main reason why I added this question is possibility to use [@Pattern](http://docs.oracle.com/javaee/6/api/javax/validation/constraints/Pattern.html) to ckeck that input string is not blank. So I do not want to use find() function. I can create my own annotation and write a validator, but I asked to check is there solution in "plain" rexexp or not. – Cherry Aug 27 '13 at 04:32
  • @Cherry Of course it doesn't. As my answer says, this finds all strings that consist of all _non-blank_ characters, where there is at least one of these. – Gene Aug 27 '13 at 05:33
  • @Cherry Ok I added a regex that uses `matches` to match a line with at least one non-blank in it. – Gene Aug 27 '13 at 05:51
1

How about this:

if(!string.trim().isEmpty()) {
    // do something
}
Sethiel
  • 182
  • 6
  • 14
0

Use regex \s : A whitespace character: \t\n\x0B\f\r.

Pattern.compile("\\s")
Ankur Lathi
  • 7,636
  • 5
  • 37
  • 49