Check string contains whitespace along with some other char sequence using regex in java
Solution 1
^(.*\s+.*)+$
seems to work for me. Accepts anything as long as there is at least one space in the string. This will match the entire string.
If you only want to check for the presence of a space, you can just use \s
without any begin or end markers in the string. The difference is that this will only match the individual spaces.
Solution 2
Your regex is not correct.
That's a string representing a regular expression. (as tchrist pointed out correctly)
The corresponding pattern that you get when using Pattern.compile() matches only strings containing one or more whitespace characters, starting from the beginning until the end. Thus, the matching string only consists of whitespace characters.
Try this string instead for Pattern.compile():
"\\s+"
The difference is that without the anchors "^" and "$" there may be other characters around the whitespace character. The whitespace character(s) may be everywhere in the string.
Using this pattern-string the whitespace character(s) must be at the beginning:
"^\\s+"
And here the sequence of whitespace characters has to be at the end:
"\\s+$"
Related videos on Youtube
Romi
Updated on July 05, 2022Comments
-
Romi almost 2 years
am using regex expression to check if a string contains white space. my regex is :
^\\s+$
for example if my string is
my name
then regex matches should return true. but it is returning true only if my string contains only spaces no other character.How to check if a string contains a whitespace or tab or carriage return characters in between/start/end of some string.
-
Anthony Grist about 12 yearsIt will be much easier to understand exactly what you want if you provide examples of what should and shouldn't match.
-
tchrist about 12 yearsYour regex better not have a double backslash.
-
Romi about 12 yearsas i have already mentioned in post, if string is "my name" i dont want this, it should be only myname. a string without any whitespace chars.
-
AlexS about 12 yearsThe double backslash is needed in this place, because he uses the \s-character class and not a "escaped" s (which doesn't even exist I think).
-
tchrist about 12 years@AlexS No, that’s not right. A pattern never has double backslashes. That’s just an artifact of the compiler. The pattern has to not have them.
-
AlexS about 12 years@tchrist That's true, the pattern doesn't have two backslashes, but to specify it with Pattern.compile() one needs to escape the backslash. If the question had asked only about regular expressions not mentioning java I wouldn't have used an escaped backslash. This way I provided ready to use strings.
-
-
Alexey Berezkin about 12 yearsTry to use simply
\s
as a regex. -
tchrist about 12 yearsThose are strings, not regexes.
-
tripleee about 12 yearsThat's superfluous. Simply
\s
does that. You are basically adding a check for beginning and ending of string, but all strings have beginnings and ends. -
Dervall about 12 years@tripleee I'm assuming the guy wants the entire string matched, i.e. that using it with
my name
will match the entire string. Using a lone\s
will match if there's a space in the string, but it will only match that character. I interpreted the question as "Match any string as long as there at least one whitespace character inside it". -
tripleee about 12 yearsThere are no capturing parens in the question. Maybe you want to clarify this aspect of your answer.
-
AlexS about 12 yearsI'm sorry to disappoint you, but they are. Remember that the strings are ready to use in Java-programs (Backslash is escaped) and as you can see this is a java-regex question. Please try things out, before you post things like this. These strings are tested and do exactly what I wrote.
-
AlexS about 12 yearsOk, let's be more precise: What I meant was "strings representing patterns". I corrected it in my answer as well. I don't know if my grandma ever did anything like that, but I also don't know what sucking eggs stands for (non native english speaker). I only know that she never started counting beans ;)