I need help checking my Regexp.
Context:
I have a script that takes an Excel file as an input to import products in a DB, there's a special field that takes "extra keys" for the product, the product can have 3 extra keys and they have to be separated by comma.
So the Regexp I made for checking the "extra keys" field is
/^((?!0+,?$)(?! +,?$)[A-Z0-9 ]{1,20},?){0,3}$/
So I can later split it in the server by "," (comma), the spaces are accepted in the Regexp because I intend to replace all spaces by empty string before the split
My question is, is this regexp OK?
I have used regular expressions but this is the first time I use negative lookahead expressions.
If its not correct, can you help me getting the right regexp?
Thank you!
--- Edit ---
Well as I just told in a comment in the accepted answer by Reilas, my RegExp is not correct at all since it accepts 0s + spaces, so when I replaced spaces for empty strings I would get 0s, which is invalid for my case.
If the keys can only have letters and numbers and can not contain only zeroes, and the other accepted characters in the string are commas between optional spaces, you can use a single negative lookahead to rule out only zeroes between 2 word boundaries:
^(?!.*?\b0+\b) *[A-Z\d]{1,20}(?: *, *[A-Z\d]{1,20}){2} *$
The pattern matches:
^
Start of string(?!.*?\b0+\b)
Negative lookahead, assert not only zeroes between word boundaries *[A-Z\d]{1,20}
Match optional spaces, and then 1-20 chars A-Z or digits(?:
Non capture group
*, *
Match a comma between optional spaces[A-Z\d]{1,20}
Match 1-20 chars A-Z or digits){2}
Close the non capture group and repeat it 2 times *
Match optional spaces$
End of stringSee a regex demo.
If there are no spaces allowed:
^(?!.*?\b0+\b)[A-Z\d]{1,20}(?:,[A-Z\d]{1,20}){2}$