Regular expressions for one or more telephone numbers

Asked

Viewed 175 times

2

I have a regular expression that validates mobile phone and landline, but the user can only add a fixed or a mobile phone, it does not allow to add more than one number.

How do I for expression accept more than one number?

I can even get her to accept more than one number if I set her within a group and repeat, only then she accepts if I put two numbers, if I put 1 does not accept, and I needed her to accept 1 or more numbers.

/^[1-9]{2} (?:[2-8]|9[1-9])[0-9]{3}\-[0-9]{4}$/

Example: 11 91111-1111


I use a tool to create requests for users. It was requested to create an option for the user to enter one or more phones, in this format: "11 91111-1111".

This tool has an option that I can use regex. The expression I am using above is correct, however, it only allows the user to enter only one number.

I tried that way too:

(/^[1-9]{2} (?:[2-8]|9[1-9])[0-9]{3}\-[0-9]{4}$/) (/^[1-9]{2} (?:[2-8]|9[1-9])[0-9]{3}\-[0-9]{4}$/)

But then it only allows the user to enter two numbers, not less or more than that.

  • And how would the examples with two or more phone numbers?

  • 2

    Don’t get it, you want to find one or more numbers inside an input? is in javascript that this is applied?

  • I use a tool to create requests for users. It was requested to create an option for the user to enter one or more phones, in this format:11 91111-1111. This tool has an option that I can use regex. The expression I’m using that I put in the creation of the post is correct, however, it only allows the user to enter only one number. I tried this way too: (/ [1-9]{2} (?:[2-8]|9[1-9])[0-9]{3}-[0-9]{4}$/) (/ [1-9]{2} (?:[2-8]|9[1-9])[0-9]{3}-[0-9]{4}$/), but then it only allows the user to enter two numbers no less or more than that

1 answer

3

The correct regex depends on how the numbers will be accepted. For example, if you want them to be on the same line and be separated by space, an option would be:

^[1-9]{2} (?:[2-8]|9[1-9])[0-9]{3}\-[0-9]{4}( [1-9]{2} (?:[2-8]|9[1-9])[0-9]{3}\-[0-9]{4})*$

This regex basically boils down to:

^regex_numero( regex_numero)*$

That is, the regex for the number is the same that you used ([1-9]{2} etc...). The idea is that I have an occurrence of this regex (in case I only have a number), and then I can have ( regex_numero)*.

Notice that just after the ( there is a space. This indicates that after the first phone number you can have a space, followed by another number. I group all of this in parentheses and use the quantifier *, which means "zero or more occurrences". This means that this whole group (space + another phone number) can repeat itself zero or more times.

Therefore, regex accepts one or more telephone numbers in the given format, separated by space. Examples of accepted cases:

11 91129-1231
11 91231-2343 11 2322-4332
15 98221-9540 12 93853-2343 45 4332-0593

See the regex working on regex101.com.


Another important detail is the use of markers ^ and $, which are respectively the beginning and end of the string. So I guarantee that the string only has what is in regex (so if there is any other character before or after the numbers, the regex fails).


If you want to limit the amount, just change the * by any of the quantifiers {min,max}. Ex:

^[1-9]{2} (?:[2-8]|9[1-9])[0-9]{3}\-[0-9]{4}( [1-9]{2} (?:[2-8]|9[1-9])[0-9]{3}\-[0-9]{4}){0,3}$

Instead of the *, I used {0,3} (at least zero, at most 3 occurrences), which means that the "space + number" sequence can be repeated at most 3 times. So now regex accepts between 1 and 4 phone numbers, separated by space.


Repeating the entire regex can be somewhat annoying and even cause future maintenance problems (as a change in regex would require a 2-place change).

If the tool you use supports subroutines, it is possible to do something like this:

^(?<numero>[1-9]{2} (?:[2-8]|9[1-9])[0-9]{3}\-[0-9]{4})( (?&numero))*$

The syntax (?<numero> defines a named group whose name is numero. Next, (?&numero) meaning "the same regex as defined in named group numero". So you don’t need to repeat the same regex twice (see here her working).

Another option is to use unnamed groups:

^([1-9]{2} (?:[2-8]|9[1-9])[0-9]{3}\-[0-9]{4})( (?1))*$

In this case, the first number is in parentheses, and this can be referenced later through (?1) (which references the first pair of parentheses - in this case, the expression which corresponds to a telephone number).

It is important to note that subroutines refer to the same expression (and can therefore give match in different numbers), different from what happens with capture groups, which reference the stretch that was captured (and would only give match if the same number repeated).

But this subroutine feature is not supported on all Engines/languages/tools (and in this case, the way is to repeat the expression itself).

Browser other questions tagged

You are not signed in. Login or sign up in order to post.