Hi Have the Following Regex written.
(?<!x)(\d{9}|\d{3}-\d{6}|\d{3}-\d{3}-\d{3})
I want to modify the existing Regex so that the below use cases can get a match, right now only few are matching.
Note:
- 9 digit Order numbers should not get detected if 'X or x' precedes the number and that part is working fine.
- 9 digit numbers, Non-numeric characters or whitespaces (up to 3) in between numbers should also get matched.
Use cases:
This is a list of Use cases that needs to be Matched
https://regex101.com/r/iFiwx5/1
Example: These are the use cases that needs to be matched with the regex.
- 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 would be really good.
CodePudding user response:
If I understand problem correct, you may use this regex:
(?<!x)(?=(?:[._ –-]*\d){9})\d{2,}[._ –-]*\d{2,}[._ –-]*\d{2,}
Explanation:
(?<!x)
Make sure digits are not preceded with letterx
(?=(?:[._ –-]*\d){9})
ensures presence of at least 9 digits separated with 0 or more allowed delimiters[._ –-]*
: allows for presence of 0 or more of these delimiters every 2 or more digits