1

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 !!

0

3 Answers 3

2

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.

Sign up to request clarification or add additional context in comments.

6 Comments

This is working to match, what about a solution that enables searching?
@mozway This comment is irrelevant to the current requirements. The OP question is about validation and not extraction from longer text.
Thus my question, I am curious if you had an answer to that ;)
@mozway Then please go ahead and post it. I do not know your exact requirements.
Thanks..it solved my problem. But can you please explain me (?=.{12}$) how this is working ?
|
1

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.

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.