Reputation: 109
I have a requirement where I need to match string which satisfies all of the below requirements -
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 !!
Upvotes: 1
Views: 1522
Reputation: 89565
Use a non-word boundary \B
:
^(?:[a-zA-Z0-9]|\s\B){12}$
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.
Upvotes: 1
Reputation: 626950
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.
Upvotes: 2
Reputation: 785316
You may use this regex:
^(?!.*\h\S)[\da-zA-Z\h]{12}$
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$
: EndUpvotes: 0