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

3 Answers3

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.

Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563
  • This is working to match, what about a solution that enables searching? – mozway Dec 01 '21 at 15:35
  • @mozway This comment is irrelevant to the current requirements. The OP question is about validation and not extraction from longer text. – Wiktor Stribiżew Dec 01 '21 at 15:36
  • Thus my question, I am curious if you had an answer to that ;) – mozway Dec 01 '21 at 15:37
  • @mozway Then please go ahead and post it. I do not know your exact requirements. – Wiktor Stribiżew Dec 01 '21 at 15:38
  • Thanks..it solved my problem. But can you please explain me (?=.{12}$) how this is working ? – Suraj Pandey Dec 01 '21 at 18:12
  • @SurajPandey See [Restricting Character length in Regular expression](https://stackoverflow.com/a/32477224/3832970). It is a positive lookahead, it just checks if its pattern can eb matched immediately on the right. If it matches, matching can proceed, else, the match is failed. – Wiktor Stribiżew Dec 01 '21 at 19:10
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.

Casimir et Hippolyte
  • 88,009
  • 5
  • 94
  • 125
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
anubhava
  • 761,203
  • 64
  • 569
  • 643