Home computickets How to set the limit number of characters in regular expression?

How to set the limit number of characters in regular expression?

Author

Date

Category

It is worth such a task: write a regular, which checks the phone number on validity.

Conditions:

  • In the room there may be a ‘+’
  • character

  • room may contain 0-2 characters ‘-‘, which can not go in a row
  • number may contain 1 pair of brackets ‘(‘ and ‘)’, and if it
    There is, then it is located to the left of the signs ‘-‘, in brackets and between ‘-‘ should be 1+ characters
  • And the most important number must be strictly 10 characters.

I got this:

/ ^ \ +? \ d * (\ (\ d + \))? \ D * ((\ - \ D + \ -) ​​| \ -?)? \ D * /

I can’t understand how to set a specific number of characters. That is, I do not know, in which particular place will be ‘(‘ or ‘-‘ and whether they will be at all. And at the same time, such a question is, how to improve (reduce) this expression?

How to solve the task without regular, I imagine, but I want to figure it out in the subtleties of regular expressions. A quick search on this topic did not give anything.


Answer 1, Authority 100%

There is a very simple technique that allows you to easily describe such regular expressions.
The essence is enclosed in the following – first all possible characters are allowed, and then using the positional checks, this sequence is verified for compliance with the rules, with one positional checking – one rule that significantly simplifies understanding and changing such regular expressions.

That is, first we allow all the permissible characters in the right amount, in a particular case it looks like this:

^ \ +? (?: [() -] * \ d) {10} [() -] * $
// 10 numbers, and between them can be brackets and dash in any quantity and order

Next, select clear rules that can be described in separate positional checks:

  1. In the room there may be a +
    I do not take into account this rule, because you just need to insert \ +? to the top of the entire regular expression, and all other checks insert after this design.
  2. Number may contain 0-2 characters ‘-‘

    (?! (?:. * -) {3})
    
  3. signs - can not go in a row

    (?!. * -)
    
  4. There may be only one pair of brackets ()

    (? = [^ ()] * \ ([^ ()] + \) [^ ()] * $ | [^ ()] * $)
    
  5. brackets necessarily left -

    (?!. * -. * [()])
    
  6. between at least 1 symbol: an excess rule, as it is equivalent to the absence of - (with brackets, the presence of one symbol is implemented in 4 )

Now we combine all this in one regular expression:

^ \ +? (?! (?:. * -) {3}) (?! * - -) (? = [^ ()] * \ ([^ ()] + \) [^ ()] * $ | [^ ()] * $) (?! * -. * [()]) (?: [() -] * \ d) {10} [() -] * $

https://regex101.com/r/lb8ew1/1

Regular expression is collected solely based on the rules, which are described in the text of the question, if it is to find something wrong, then the rules are not described in full.


Answer 2, Authority 133%

answer to the question: How to check if 10 digits are exactly in the presented room?

/ (? = (^ ([^ \ d] *? \ d) {10} $)) /

Test https://regex101.com/r/hn2xk6/1

  1. You can combine this regular expression with your regular expression so

    / (? = (^ ([^ \ d] *? \ d) {10} $)) ^ \ +? \ D * (\ (\ d + \))? \ D * ((\ - \ d + \ -) ​​| \ -?)? \ D * $ /
    

    Test https://regex101.com/r/on3rw4/1 (in your expression The gaps are prohibited, unlike the regular expression @saidolim djuraev)

  2. Regular expression requires a number at the end of the line, unlike your expression.


Answer 3

I think you will help the following design

/ (\ + \ d {1,2} \ s)? \ (? \ D {3} \ )? [\ s .-] \ d {3} [\ s .-] \ d {4} /

Suitable options like

123-456-7890
(123) 456-7890
123 456 7890.
+91 (123) 456-7890

just do not know about 10 characters. Since many different situations

Programmers, Start Your Engines!

Why spend time searching for the correct question and then entering your answer when you can find it in a second? That's what CompuTicket is all about! Here you'll find thousands of questions and answers from hundreds of computer languages.

Recent questions