Home > OS >  Regex pattern for string having spaces only at end
Regex pattern for string having spaces only at end

Time:12-02

I have a requirement where I need to match string which satisfies all of the below requirements -

  1. String must be of length 12
  2. String can have only following characters - Alphabets, Digits and Spaces
  3. Spaces if any must be at the end of the string. Spaces in between are not allowed.

I have tried with below regex -

"^[0-9a-zA-Z\s]{12}$"

Above regex is satisfying requirement #1 and #2 but not able to satisfy #3. Please help me to achieve the requirements. Thanks in advance !!

CodePudding user response:

You can use

^(?=.{12}$)[0-9a-zA-Z]*\s*$

If at least one letter must exist:

^(?=.{12}$)[0-9a-zA-Z] \s*$

Details:

  • ^ - start of string
  • (?=.{12}$) - the string must contain 12 chars
  • [0-9a-zA-Z]* - zero or more alphanumeroics
  • \s* - zero or more whitespaces
  • $ - end of string.

See the regex demo.

CodePudding user response:

You may use this regex:

^(?!.*\h\S)[\da-zA-Z\h]{12}$

RegEx Demo

RegEx Details:

  • ^: Start
  • (?!.*\h\S): Negative lookahead to fail the match if a whitespace is followed by a non-whitespace character
  • [\da-zA-Z\h]{12}: Match 12 characters of alphanumerics or white space
  • $: End

CodePudding user response:

Use a non-word boundary \B:

^(?:[a-zA-Z0-9]|\s\B){12}$

demo

With it, a space can't be followed by a letter or a digit, but only by a non-word character (a space here) or the end of the string.

To ensure at least one character that isn't blank:

^[a-zA-Z0-9](?:[a-zA-Z0-9]|\s\B){11}$

Note that with PCRE you have to use the D (DOLLAR END ONLY) modifier to be sure that $ matches the end of the string and not before the last newline sequence. Or better replace $ with \z. There isn't this kind of problem with Python and the re module.

  • Related