Regular expression for letters, one space between words and total length 50

I am trying to create a regex with the following conditions:

  • Letters only
  • One space between words (no more than one space)
  • Maximum length 50

And here's what I've done so far:

^(([A-Za-z]+( [A-Za-z])+){1,50})$

      

This allows me to check the spaces between words and terms only letters, but it does not work for long and does not work for the words without spaces, for example: hello

. Can anyone help me with this?

Example:

What I need: A Regex that allows sentences (with a maximum length of 50) like this:

Hello this is an example
Hello
a b c

      

+3


source to share


1 answer


Try the following:

^(?!.*?\s{2})[A-Za-z ]{1,50}$

      



Demo

[A-Za-z ]{1,50}

will check for characters and length, whereas lookahead will (?!.*?\s{2})

check for whitespace condition.

+4


source







All Articles