-1

Is it possible to check with regex:

  1. the complete string are numbers AND
  2. the first character is a 7 or 8 then the complete length of the string must be 11 OR
  3. the first character is a 1 then the complete length of the string must be 10
  4. OR the first character is a 0 then the complete length of the string must be 18 AND on character 8 must be a 8 or 7 OR on character 9 must be a 1

I hope you can see what I mean. Hope the examples will help you to know what I mean.

Here is my solution(not working completely-> I don't know how to check if in case it starts with a 0 and it is 18 characters long the character on position 8 must be 7or8 or on position 9 the character must be 1):

^(?:[78]\d{10}|[1-69]\d{9}|[0]/d{18})$

For example:

  • 85556987456 -> starts with 8 and length is 11 -> match
  • 75556987456 -> starts with 7 and length is 11 -> match
  • 1555698745 -> starts with 1 and length is 10 -> match
  • 000000085556987456 -> starts with 0 and length is 18 and on pos 8 is a 8 -> match
  • 000000075556987456 -> starts with 0 and length is 18 and on pos 8 is a 7 -> match
  • 000000001556987456 -> starts with 0 and length is 18 and on pos 9 is a 1 -> match

Thank you!

InFlames82
  • 493
  • 6
  • 17

3 Answers3

1
(?=^\d+)^([78]\d{10}|[1]\d{9}|[0](?:\d{6}[78]|\d{7}[1])(?:\d{10}|\d{9}))$

https://regex101.com/r/8igywV/2

rootkonda
  • 1,700
  • 1
  • 6
  • 11
0

It is certainly possible with a single regex, but it is not going to be readable. Write regular expressions for all sub-constraints and implement all "OR" as a regex alternatation (|):

  • the first character is a 7 or 8 then the complete length of the string must be 11: ^[78][0-9]{10}$
  • the first character is a 1 then the complete length of the string must be 10: ^1[0-9]{9}$
  • the first character is a 0 then the complete length of the string must be 18 AND on character 8 must be a 8 or 7 OR on character 9 must be a 1: ^0([0-9]{6}[78][0-9]{10}|[0-9]{7}1[0-9]{9})$

becomes combined:

^[78][0-9]{10}$|^1[0-9]{9}$|^0([0-9]{6}[78][0-9]{10}|[0-9]{7}1[0-9]{9})$

the start-of-input anchor could be extracted since it is the same for all alternatives:

^([78][0-9]{10}|1[0-9]{9}|0([0-9]{6}[78][0-9]{10}|[0-9]{7}1[0-9]{9}))$

But since regular expressions are mostly used from within another "host language", I suggest doing the "or" checks not in the regex, but in the host language. At the same time, the constraint "all characters must be digits" can be validated separately, which simplifies the regex further. E.g.:

if (input.match(/^[0-9]+$/) == null) {
  // all-digits constriant violated
  return;
}

if (input.match(/^[78].{10}$/)
  || input.match(/^1.{9}$/)
  || input.match(/^0.{6}[78].{10}$/)
  || input.match(/^0.{7}1.{9}$/)) {
  // input matched!
}
knittl
  • 246,190
  • 53
  • 318
  • 364
0

You can use this regex: ^([78]\d{10}|1\d{9}|0\d{6}[78]\d{10}|0\d{7}1\d{9})$

^ start of input

[78]\d{10} start with 7 or 8 followed by 10 more numbers

OR

1\d{9} start with 1 followed by 9 more numbers

OR

0\d{6}[78]\d{10} start with 0, followed by 6 numbers, then 7 or 8, then 10 more numbers

OR

0\d{7}1\d{9} start with 0, then 7 numbers, then 1 and then 9 more numbers

$ end of input

Demo: https://regex101.com/r/p3fyl3/1

Alex G
  • 1,897
  • 2
  • 10
  • 15