Anand B
Anand B

Reputation: 3085

Regex to check for string with 2 or more spaces

I tried to write a regex for validating following string, good <3 spaces> morning

The regex I tried was \s{2,}. However the regex match failed

Upvotes: 0

Views: 121

Answers (3)

Andreas Wederbrand
Andreas Wederbrand

Reputation: 39951

The string must match entirly

"Good   morning".matches(".*\\s{2}.*");

Upvotes: 2

TheBlastOne
TheBlastOne

Reputation: 4320

How about "\Agood morning\Z"?

It might seem too obvious, but it works.

Upvotes: 0

lc2817
lc2817

Reputation: 3742

".*\s\s.*"
  • Any sequence of character (potentially empty)
  • a space
  • a space
  • any sequence of character(potentially empty)

Upvotes: -1

Related Questions