Please tell me which pattern I need to use if field must not contain only spaces.
-
"\s+" means "one or more whitespaces of any kind". – duffymo Sep 24 '12 at 15:48
-
Case is so important. Corrected. – duffymo Sep 24 '12 at 15:51
-
nobody understand my question.. – Kirill Bazarov Sep 25 '12 at 08:33
5 Answers
Why need to use regex?
str.trim().isEmpty()
- 154,648
- 35
- 237
- 264
-
ok i did it a little bit another way - str.replace(" ","") and then isEmpty() – Kirill Bazarov Sep 25 '12 at 08:33
You must use this pattern.
.*[^ ].*
It can be anything but not only spaces.
- 369
- 3
- 11
assuming you mean any whitespace, not just spaces, \S will work.
Cfr every dev's must-have friend, The regex cheat sheet
- 16,239
- 4
- 38
- 53
-
field could be "Hi i am" but could not be " ". It can contain spaces but not only spaces – Kirill Bazarov Sep 24 '12 at 15:48
-
@KirillBazarov: If your string matches \S it means that it has at least one character that is not a whitespace. It still can contain whitespaces. – Daniel Hilgarth Sep 24 '12 at 15:50
-
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.
- 625
- 8
- 13
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/