I'm trying to write a PCRE Regex pattern to match only the numbers of length (8 or 9 or 12) while leaving the last 4 digits unmasked. Below is my written regex pattern.
/(?<=\D|^)(?=\d{8,12}\D|$)(\d{3})[\s,-]?\[\s,-]\K\d{4}|(?<=\D|^)(?=\d{12}|\d{9}\D|$)\d{5}/gmi
Reference Regex101 link : https://regex101.com/r/DukWNG/1 This link has the working test cases for understanding.
Right now it matches the first 5 digits only but for length 8 or 9 or 12, it should match all the digits except leaving the last 4 digits.
Test case :
- if the length of the number is 8, then it should match first 4 leaving the last 4 unmasked
- if the length of the number is 9, then it should match first 5 leaving the last 4 unmasked
- if the length of the number is 12, then it should match first 8 digits leaving the last 4 unmasked.
and it should also match if there is a "hyphen" or "comma" or "dash" or "space" between numbers. I'm stuck and not sure how to make this work. Any help would be really great.