-1

I have this regex (^[a-z]\s{1})+. It is supposed to match only letters that are separated by a single space but it still returns true even with this input:

"a a    a"

I want it to reject the expression if it has two consecutive spaces. Can you help me?

Chowder
  • 41
  • 2
  • 9

1 Answers1

1

This is a regex that fixes this: ^\s*([a-z]\s|[a-z]$)+\s*$

Explanation:

  • It matches the string up to the end ($) so it will discard your example line
  • It matches the case when the string ends with a letter - [a-z]$
  • It ignores leading and trailing spaces: \s* at the beginning and the end

You can play with this here.

Tamas Rev
  • 6,418
  • 4
  • 28
  • 47