I'm trying to write a regex pattern for Australian driver's license validation.
The criteria for the license are:
Eight-, nine-, or 10-digit number, or a six-digit alphanumeric pattern that matches the Australia Driver's license number format. It also checks for common test numbers, and requires the presence of related keywords.
Patterns:
\d\d\d \d\d\d \d\d\d \d\d \d\d\d \d\d\d [A-Za-z]\d\d\d\d\d \d\d\d[-]\d\d\d[-]\d\d\d\d
Data ending with any of the following list of values is not matched: 00000, 11111, 22222, 33333, 44444, 55555, 66666, 77777, 88888, 99999
Regex I've come up with so far:
*For only numeric patterns
[0-9]{3}-?[0-9]{3}-?[0-9]{4}
not able to use negate case for data ending in repeating characters:
^(([0-9]){4}\1 $)
Can someone help coming up with regex that matches ALL of the above criteria (inclusion and exclusion case)? I'm confused how how account for 6 digit alphanumeric or 6-10 digits numeric with "-" in between. Also I was able to figure out how to determine repeating characters in the end with pattern:
(([0-9]){4}\1 $)
but not how to write regex to not match them. I tried ^(([0-9]){4}\1 $)
but that did not work.
CodePudding user response:
/\b\d{3}[-]\d{3}[-]\d{4}\b|\b\d{2,3} \d{3} \d{3}\b|\b[A-Za-z](\d)(?!\1{4})\d{4}\b/g
Segment | Description |
---|---|
\b\d{3}[-]\d{3}[-]\d{4}\b |
Non-word on the left and a word on the right of a zero space character, then 3 digits, then a dash - , then 3 digits, then a dash - , then 4 digits, then a word on the left and a non-word to the right of a zero space character. |
| |
OR |
\b\d{2,3} \d{3} \d{3}\b |
Non-word on left and a word on the right of a zero space character, then 2 to 3 digits, then a space, then 3 digits, then a space, then 3 digits, then a word on the left and a non-word on the left of a zero space character. |
| |
OR |
\b[A-Za-z](\d)(?!\1{4})\d{4}\b |
Non-word on the left and a word on the right of a zero space character, then a letter (of any case), then a capture group of 1 digit, this will only match if the there ISN'T 4 more of the digit found in the previous capture group, then 4 digits, and finally a word on the left and a non-word on the right of a zero space character. |
A slicker version by The fourth bird wraps everything in \b(?:
...)\b
which cuts down on the code size of my RegEx by 11 characters.
/\b(?:\d{3}-\d{3}-\d{4}|\d{2,3} \d{3} \d{3}|[A-Za-z](\d)(?!\1{4})\d{4})\b/g
const str = `
987 654 321
10 203 405
A12345
123-456-7890
x00000
888 777-999
111-2222-333
741 852 9630`;
const rgx = /\b\d{3}[-]\d{3}[-]\d{4}\b|\b\d{2,3} \d{3} \d{3}\b|\b[A-Za-z](\d)(?!\1{4})\d{4}\b/g;
console.log(str.match(rgx));