3

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

Vikdor
  • 23,470
  • 10
  • 58
  • 82
Kirill Bazarov
  • 4,506
  • 6
  • 32
  • 53

5 Answers5

17

Why need to use regex?

str.trim().isEmpty()
xdazz
  • 154,648
  • 35
  • 237
  • 264
12

You must use this pattern.

.*[^ ].*

It can be anything but not only spaces.

Caner Korkmaz
  • 369
  • 3
  • 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,239
  • 4
  • 38
  • 53
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
  • 625
  • 8
  • 13
0

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

patern with no blank :

^[patern]*[patern without space][patern]*$

Exemple:

-patern: 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 space at all you don't need to do that.

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

Infern0
  • 2,296
  • 1
  • 7
  • 17
UnDev
  • 9
  • 2