Regex - matching brazilian phone numbers
up vote
2
down vote
favorite
I'm trying to use Regex to validate brazilian phone numbers.
For some reason, I cannot use b to indicate the beginning and the end for the match. In this case, my Regex selects some invalid phone numbers as valid.
RegEX:
(?([0-9]{2,3}|0{1}((x|[0-9]){2,3}[0-9]{2})))?s*[0-9]{4,5}[- ]*[0-9]{4}
Valid numbers:
(00)0000-0000; true
(00)000000000; true
(00) 00000 0000; true
00 00000 0000; true
(000)00000 0000; true
Invalid numbers:
23232443243243423432; true
(0000)000000000; true
a00 00000-0000 ; true
Thanks!
javascript regex
|
show 1 more comment
up vote
2
down vote
favorite
I'm trying to use Regex to validate brazilian phone numbers.
For some reason, I cannot use b to indicate the beginning and the end for the match. In this case, my Regex selects some invalid phone numbers as valid.
RegEX:
(?([0-9]{2,3}|0{1}((x|[0-9]){2,3}[0-9]{2})))?s*[0-9]{4,5}[- ]*[0-9]{4}
Valid numbers:
(00)0000-0000; true
(00)000000000; true
(00) 00000 0000; true
00 00000 0000; true
(000)00000 0000; true
Invalid numbers:
23232443243243423432; true
(0000)000000000; true
a00 00000-0000 ; true
Thanks!
javascript regex
If you match the whole string by enclosing the whole regex with^
and$
, will it be OK?
– Wiktor Stribiżew
Nov 21 at 17:15
Parentheses, spaces and dashes don't actually mean anything as far as telephone devices are concerned, you could very well strip those from the string before validating.
– Havenard
Nov 21 at 17:16
@Havenard, I'm using this Regex as Watson Assistant pattern. That's the problem.
– Zeca Novaes
Nov 21 at 17:19
@WiktorStribiżew Not working :(
– Zeca Novaes
Nov 21 at 17:19
1
Try(?(b[0-9]{2,3}|0((x|[0-9]){2,3}[0-9]{2})))?s*[0-9]{4,5}[- ]*[0-9]{4}b
. See demo.
– Wiktor Stribiżew
Nov 21 at 17:21
|
show 1 more comment
up vote
2
down vote
favorite
up vote
2
down vote
favorite
I'm trying to use Regex to validate brazilian phone numbers.
For some reason, I cannot use b to indicate the beginning and the end for the match. In this case, my Regex selects some invalid phone numbers as valid.
RegEX:
(?([0-9]{2,3}|0{1}((x|[0-9]){2,3}[0-9]{2})))?s*[0-9]{4,5}[- ]*[0-9]{4}
Valid numbers:
(00)0000-0000; true
(00)000000000; true
(00) 00000 0000; true
00 00000 0000; true
(000)00000 0000; true
Invalid numbers:
23232443243243423432; true
(0000)000000000; true
a00 00000-0000 ; true
Thanks!
javascript regex
I'm trying to use Regex to validate brazilian phone numbers.
For some reason, I cannot use b to indicate the beginning and the end for the match. In this case, my Regex selects some invalid phone numbers as valid.
RegEX:
(?([0-9]{2,3}|0{1}((x|[0-9]){2,3}[0-9]{2})))?s*[0-9]{4,5}[- ]*[0-9]{4}
Valid numbers:
(00)0000-0000; true
(00)000000000; true
(00) 00000 0000; true
00 00000 0000; true
(000)00000 0000; true
Invalid numbers:
23232443243243423432; true
(0000)000000000; true
a00 00000-0000 ; true
Thanks!
javascript regex
javascript regex
edited Nov 21 at 18:12
Poul Bak
5,38331132
5,38331132
asked Nov 21 at 17:12
Zeca Novaes
14510
14510
If you match the whole string by enclosing the whole regex with^
and$
, will it be OK?
– Wiktor Stribiżew
Nov 21 at 17:15
Parentheses, spaces and dashes don't actually mean anything as far as telephone devices are concerned, you could very well strip those from the string before validating.
– Havenard
Nov 21 at 17:16
@Havenard, I'm using this Regex as Watson Assistant pattern. That's the problem.
– Zeca Novaes
Nov 21 at 17:19
@WiktorStribiżew Not working :(
– Zeca Novaes
Nov 21 at 17:19
1
Try(?(b[0-9]{2,3}|0((x|[0-9]){2,3}[0-9]{2})))?s*[0-9]{4,5}[- ]*[0-9]{4}b
. See demo.
– Wiktor Stribiżew
Nov 21 at 17:21
|
show 1 more comment
If you match the whole string by enclosing the whole regex with^
and$
, will it be OK?
– Wiktor Stribiżew
Nov 21 at 17:15
Parentheses, spaces and dashes don't actually mean anything as far as telephone devices are concerned, you could very well strip those from the string before validating.
– Havenard
Nov 21 at 17:16
@Havenard, I'm using this Regex as Watson Assistant pattern. That's the problem.
– Zeca Novaes
Nov 21 at 17:19
@WiktorStribiżew Not working :(
– Zeca Novaes
Nov 21 at 17:19
1
Try(?(b[0-9]{2,3}|0((x|[0-9]){2,3}[0-9]{2})))?s*[0-9]{4,5}[- ]*[0-9]{4}b
. See demo.
– Wiktor Stribiżew
Nov 21 at 17:21
If you match the whole string by enclosing the whole regex with
^
and $
, will it be OK?– Wiktor Stribiżew
Nov 21 at 17:15
If you match the whole string by enclosing the whole regex with
^
and $
, will it be OK?– Wiktor Stribiżew
Nov 21 at 17:15
Parentheses, spaces and dashes don't actually mean anything as far as telephone devices are concerned, you could very well strip those from the string before validating.
– Havenard
Nov 21 at 17:16
Parentheses, spaces and dashes don't actually mean anything as far as telephone devices are concerned, you could very well strip those from the string before validating.
– Havenard
Nov 21 at 17:16
@Havenard, I'm using this Regex as Watson Assistant pattern. That's the problem.
– Zeca Novaes
Nov 21 at 17:19
@Havenard, I'm using this Regex as Watson Assistant pattern. That's the problem.
– Zeca Novaes
Nov 21 at 17:19
@WiktorStribiżew Not working :(
– Zeca Novaes
Nov 21 at 17:19
@WiktorStribiżew Not working :(
– Zeca Novaes
Nov 21 at 17:19
1
1
Try
(?(b[0-9]{2,3}|0((x|[0-9]){2,3}[0-9]{2})))?s*[0-9]{4,5}[- ]*[0-9]{4}b
. See demo.– Wiktor Stribiżew
Nov 21 at 17:21
Try
(?(b[0-9]{2,3}|0((x|[0-9]){2,3}[0-9]{2})))?s*[0-9]{4,5}[- ]*[0-9]{4}b
. See demo.– Wiktor Stribiżew
Nov 21 at 17:21
|
show 1 more comment
1 Answer
1
active
oldest
votes
up vote
3
down vote
accepted
You should place word boundaries at the "right" places. They must placed at the first obligatory word char matching pattern and after the last obligatory word char.
(?b([0-9]{2,3}|0((x|[0-9]){2,3}[0-9]{2})))?s*[0-9]{4,5}[- ]*[0-9]{4}b
^^ ^^
See the regex demo
If you put b
before (?
, when there is a (
before a digit, the word boundary will invalidate the match if there is no word char right before the (
.
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
3
down vote
accepted
You should place word boundaries at the "right" places. They must placed at the first obligatory word char matching pattern and after the last obligatory word char.
(?b([0-9]{2,3}|0((x|[0-9]){2,3}[0-9]{2})))?s*[0-9]{4,5}[- ]*[0-9]{4}b
^^ ^^
See the regex demo
If you put b
before (?
, when there is a (
before a digit, the word boundary will invalidate the match if there is no word char right before the (
.
add a comment |
up vote
3
down vote
accepted
You should place word boundaries at the "right" places. They must placed at the first obligatory word char matching pattern and after the last obligatory word char.
(?b([0-9]{2,3}|0((x|[0-9]){2,3}[0-9]{2})))?s*[0-9]{4,5}[- ]*[0-9]{4}b
^^ ^^
See the regex demo
If you put b
before (?
, when there is a (
before a digit, the word boundary will invalidate the match if there is no word char right before the (
.
add a comment |
up vote
3
down vote
accepted
up vote
3
down vote
accepted
You should place word boundaries at the "right" places. They must placed at the first obligatory word char matching pattern and after the last obligatory word char.
(?b([0-9]{2,3}|0((x|[0-9]){2,3}[0-9]{2})))?s*[0-9]{4,5}[- ]*[0-9]{4}b
^^ ^^
See the regex demo
If you put b
before (?
, when there is a (
before a digit, the word boundary will invalidate the match if there is no word char right before the (
.
You should place word boundaries at the "right" places. They must placed at the first obligatory word char matching pattern and after the last obligatory word char.
(?b([0-9]{2,3}|0((x|[0-9]){2,3}[0-9]{2})))?s*[0-9]{4,5}[- ]*[0-9]{4}b
^^ ^^
See the regex demo
If you put b
before (?
, when there is a (
before a digit, the word boundary will invalidate the match if there is no word char right before the (
.
answered Nov 21 at 17:25
Wiktor Stribiżew
303k16123199
303k16123199
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53417334%2fregex-matching-brazilian-phone-numbers%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
If you match the whole string by enclosing the whole regex with
^
and$
, will it be OK?– Wiktor Stribiżew
Nov 21 at 17:15
Parentheses, spaces and dashes don't actually mean anything as far as telephone devices are concerned, you could very well strip those from the string before validating.
– Havenard
Nov 21 at 17:16
@Havenard, I'm using this Regex as Watson Assistant pattern. That's the problem.
– Zeca Novaes
Nov 21 at 17:19
@WiktorStribiżew Not working :(
– Zeca Novaes
Nov 21 at 17:19
1
Try
(?(b[0-9]{2,3}|0((x|[0-9]){2,3}[0-9]{2})))?s*[0-9]{4,5}[- ]*[0-9]{4}b
. See demo.– Wiktor Stribiżew
Nov 21 at 17:21