2

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!

5
  • 1
    Use: ^[78][0-9]{10}$ Aug 6, 2020 at 9:45
  • 1
    Or: ^(?:[78]\d{10}|[1-69]\d{9})$
    – JvdV
    Aug 6, 2020 at 9:59
  • Thanks a lot! Unfortunately I have forgotten one more condition. I've edited the original post
    – InFlames82
    Aug 6, 2020 at 11:05
  • Then try give it your best shot with the samples given and include your own attempt. That may get your question reopened again.
    – JvdV
    Aug 6, 2020 at 11:08
  • I edit my post with my not complete solution.
    – InFlames82
    Aug 6, 2020 at 11:26

2 Answers 2

3

You can use

^(?:[78]\d{10}|1\d{9}|0\d{6}(?:[87]\d|\d1)\d{9})$

See the regex demo

Details

  • ^ - start of string
  • (?:[78]\d{10}|1\d{9}|0\d{6}(?:[87]\d|\d1)\d{9}) - one of the three alternatives:
    • [78]\d{10} - 7 or 8 and then 10 digits (11 all in all)
    • | - or
    • 1\d{9} - 1 and then 9 digits (10 all in all)
    • | - or
    • 0\d{6}(?:[87]\d|\d1)\d{9} - 0, then 6 digits, then the 8th digit equal to 8 or 7 and any one digit, or any one digit and the 9th digit equal to 1, and then 9 more digits (=18 digits)
  • $ - end of string.
0

Here's the regex: /^[78]\d{10}$/

"^" indicates the start of the line "$" indicates the end of the line \d means digits {10}means exactly 10 [] is a group of valid values

The second question is dependant on wether your Engine supports Lookahead and Lookbehind https://www.regular-expressions.info/conditional.html

1
  • 1
    I don't think you'd need lookarounds for this right. Just an alternation would do.
    – JvdV
    Aug 6, 2020 at 10:00

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.