3

Please tell me which pattern I need to use if field must not contain only spaces.

Vikdor
  • 23,934
  • 10
  • 61
  • 84

5 Answers5

15

Why need to use regex?

str.trim().isEmpty()
xdazz
  • 158,678
  • 38
  • 247
  • 274
15

You must use this pattern.

.*[^ ].*

It can be anything but not only spaces.

Caner Korkmaz
  • 407
  • 4
  • 11
2

assuming you mean any whitespace, not just spaces, \S will work.

Cfr every dev's must-have friend, The regex cheat sheet

Joeri Hendrickx
  • 16,947
  • 4
  • 41
  • 53
2

I've got one, it seems heavy, but it's not that hard to implement.

Pattern with no blank:

^[pattern]\*[pattern without space][pattern]*$

Example:

-pattern: a-zA-ZÀ-ÿ '\-

(note that it allows spaces)

[a-zA-ZÀ-ÿ '\-]

With no blank =

^[a-zA-ZÀ-ÿ '\-]\*[a-zA-ZÀ-ÿ'\-][a-zA-ZÀ-ÿ '\-]*

Of course, if you don't allow spaces at all, you don't need to do that.

You can test yours on https://regexr.com/.

CarenRose
  • 1,266
  • 1
  • 12
  • 24
UnDev
  • 29
  • 2
0

This is much simple .If you are using java just code this. As example:-

classVariable.getname().trim().equal("")

by this you can check whether that variable contains only spaces.

Susampath
  • 706
  • 10
  • 13