Nowadays all mobile phones in Brazil have nine digits and start with digit 9 and all fixed phones have 8 digits and never start with digit 9. I would personally prefer to format the phone as (xx) xxxxx-xxxx
. So the best regular expression for this would be this:
^\([1-9]{2}\) (?:[2-8]|9[1-9])[0-9]{3}\-[0-9]{4}$
Her full explanation is:
^
- String start.
\(
- One parentheses.
[1-9]{2}
- Two digits from 1 to 9. No DDD codes with digit 0.
\)
- A close parenthesis.
- A blank space.
(?:[2-8]|9[1-9])
- The beginning of the number. Represents a choice between the one digit between 2 and 8 (the part of [2-8]
) and a 9 followed by a digit from 1 to 9 (the part of the 9[1-9]
). The |
separates the options to be chosen. O (?: ... )
groups such choices. Fixed phones start with digits from 2 to 8. Mobile phones start with 9 and have a second digit from 1 to 9. The first digit will never be 0 or 1. Mobile phones can’t start with 90 because this is the prefix for collect calls.
[0-9]{3}
- The remaining three digits of the first half of the phone number, making a total of 4 or 5 digits in the first half.
\-
- A hyphen.
[0-9]{4}
- The second half of the phone number.
$
- String end.
If you want to leave the parentheses, blank space and hyphen optional, then you can put a ?
after each of these symbols, resulting in this regular expression:
^\(?[1-9]{2}\)? ?(?:[2-8]|9[1-9])[0-9]{3}\-?[0-9]{4}$
If you want, you can also filter the Ddds that exist. Here’s an image that shows the current area codes according to the Wikipedia:
Looking at this map, the subexpression that would filter the valid Ddds would be as follows:
(?:[14689][1-9]|2[12478]|3[1234578]|5[1345]|7[134579])
As explained above, the (?: ... )
is to group. The options (separated by |
) would be two-digit groups, which would be one of the following options: 2[12478]
to Rio de Janeiro and Espírito Santo, 3[1234578]
to Minas Gerais, 5[1345]
to Rio Grande do Sul, 7[134579]
to Bahia and Sergipe and [14689][1-9]
for the rest of Brazil.
And the complete regular expression would be as follows (with the mandatory parentheses, spaces and hyphens):
^\((?:[14689][1-9]|2[12478]|3[1234578]|5[1345]|7[134579])\) (?:[2-8]|9[1-9])[0-9]{3}\-[0-9]{4}$
And with them not obligatory:
^\(?(?:[14689][1-9]|2[12478]|3[1234578]|5[1345]|7[134579])\)? ?(?:[2-8]|9[1-9])[0-9]{3}\-?[0-9]{4}$
NOTE: This answer was originally written in 2015, when there were 8 or 9 digit cell phones, depending on the area code. Since then, it has been upgraded to the newest format in effect, which always has 9 digits. If you want more details on how this response was edited, check out the edition history.
what does this escape mean
\
before space? Here...^\([1-9]{2}\)\ [2-9]...
I don’t think it’s necessary to escape a space :)– Guilherme Nascimento
@Guilhermenascimento Really. I do not remember why I had put the bar before, since it has been a few months. I think I must have got into trouble with something at the time of building the regex.
– Victor Stafusa
Victor is normal, I do it all the time, Jorgeb. He’s always following my spelling mistakes :)
– Guilherme Nascimento
@Victorstafusa, you deserve a prize for the beautiful explanation. Rather than much tutorial from regex blogs out...
– Caique C.
I use a
\s
to escape the space. That’s because I’m wearing a mask in the field and I’m sure it will always come this space.– egermano
@Victorstafusa thank you!
– egermano