Home > OS >  Regex pattern to match first 5 digit
Regex pattern to match first 5 digit

Time:03-23

I'm trying to write the regex to match only the first 5 digits instead of the entire 9 Digits of a number.

I have given the Regex below and the regex demo link.

The below regex is what I have

(?<![xX])(?=(?:[._ –-]*\d){9})(?!9|66\D*6|00\D*0|(?:\d\D*){3}0\D*0|(?:\d\D*){5}0(?:\D*0){3})\d{2,}[._ –-]*\d{2,}[._ –-]*\d{2,}

Regex Demo Here

Also, if the String "order number" precedes the 9 Digit number, the match should not take place.

Example Use cases:

Note: If the Number is preceded with 'x' or 'X' it should not get a match.

//should not get match

 1. x123456789
 2. X123456789
 3. x123-456-789
 4. X123-456-789

//if number is preceded with string "order number / order", then the number should not get matched

1. order number 123456789
 2. order.number 123456789
 3. order 123456789
 4. ordernumber 123-456-789
 5. order number 123-456789
 6. order number 123456789
 7. 123-456789
 8. 123456789
 9. ordernumber-x123456787
 10. ordernumber-123456787
 11. ordernumber - 123456789
 12. ordernumber #123456789
 13. ordernumber (anyspl charc)123456789
 14. ordernumber !@#$%^&123456789
 15. 123456789*@*#123456789
 16. social security number 123-456-789
 17. ordernumber !@#$%^&x123456789
 18. 123456789
 19. #@%#$%#$123456789#$%#$^$#
 20. order.number 123456789
 21. order_number123456789
 22. order..number 123456789
 23. order#123456789

// The Below use cases are already getting match for 9 digits, but need to get a match for only first 5 digits.

 - 123456789
 - 123 45 6789
 - 123-45-6789
 - 123-45-6789
 - 123 – 45 – 6789
 - 123.45.6789
 - 123_45_6789
 - 123 456 789
 - 123-456-789
 - 123 – 456 – 789
 - 123.456.789
 - 123_456_789
 - 1234 56 789
 - 1234-56-789
 - 1234 – 56 – 789
 - 1234.56.789
 - 1234_56_789
 - 12 345 6789
 - 12-345-6789
 - 12 – 345 – 6789
 - 12.345.6789
 - 12_345_6789

Any help on this to modify the regex above to match only the first 5 digits?

CodePudding user response:

You can use

/(?<!x)(?<!\border\s*number\W*)(?=(?:[._ –-]*\d){9})(?!9|66\D*6|00\D*0|(?:\d\D*){3}0\D*0|(?:\d\D*){5}0(?:\D*0){3})\d(?:[._ –-]*\d){4}/gi

See the regex demo. Details:

  • (?<!x) - a location not immediately preceded with x or X (the pattern is case insensitive due to i flag)
  • (?<!\border\s*number\W*) - immediately on the left, there cannot be order numberwhole word with any amount of whitespaces between the two words and any amount of non-word chars betweennumber` and the next digit
  • (?=(?:[._ –-]*\d){9}) - immediately on the right, there must be nine occurrences of zero or more dashes, underscore, space or hyphens and a digit
  • (?!9|66\D*6|00\D*0|(?:\d\D*){3}0\D*0|(?:\d\D*){5}0(?:\D*0){3}) - immediately on the right, there can be no 9, or 66 zero or more non-digits and then 6, or 00 zero or more non-digits and then 0, or three occurrences of a digit and then any zero or more non-digit chars and then 0, any zero or more non-digit chars and then a 0, or five occurrences of a digit and then any zero or more non-digit chars and then 0, and three occurrences of any zero or more non-digit chars and then a 0
  • \d - a digit is matched
  • (?:[._ –-]*\d){4} - and the four sequences of any zero or more spaces, dashes, hyphens or underscores and then a digit.
  • Related