I'm looking for a regex pattern which can do this exactly.
- Should match the length which is 12 characters alphaNumeric
- Should also check for the occurrence of hyphen - twice in the word
- No spaces are allowed.
I have tried the following regex:
^([a-zA-Z0-9]*-[a-zA-Z0-9]*){2}$
Some sample cases
-1234abcd-ab
abcd12-avc-a
-abcd-abcdacb
ac12-acdsde-
The regex should match for all the above.
And should be wrong for the below
-abcd-abcd--a
abcd-abcdefg
I've been using this regex ^([a-zA-Z0-9]*-[a-zA-Z0-9]*){2}$
for matching the above patterns, but the problem is, it doesn't have a length check of 12. I'm not sure how to add that into the above pattern. Help would be appreciated.
str.length <= 12
, if you want to do using regex you can use positive lookahead (which i will not suggest to use just for length restriction ), i.e(?=^.{12}$)
– Code Maniac