Reg ex to accept digits hyphen and parenthesis (in right order)
up vote
-1
down vote
favorite
I need a reg ex which accepts 10 to 11 digits phone number, hyphen , parenthesis (in right order) and spaces are allowed (I will remove all of the afterwards).
So all following string should be accepted by it.
12223334444
2223334444
1(222)333-4444
1(222) 333-4444
1 (222) 333-4444
1 (222) 333 - 4444
(222)333-4444
(222) 333-4444
(222) 333 - 4444
1-222-333-4444
1 - 222 - 333 - 4444
222-333-4444
222 - 333 - 4444
I have made this , but it accepts many other strings
^(.*?[.-|( |]|[0-9][^d]*(d+)[^d].* [.-|)].*)$
I am using C#.
c# regex
add a comment |
up vote
-1
down vote
favorite
I need a reg ex which accepts 10 to 11 digits phone number, hyphen , parenthesis (in right order) and spaces are allowed (I will remove all of the afterwards).
So all following string should be accepted by it.
12223334444
2223334444
1(222)333-4444
1(222) 333-4444
1 (222) 333-4444
1 (222) 333 - 4444
(222)333-4444
(222) 333-4444
(222) 333 - 4444
1-222-333-4444
1 - 222 - 333 - 4444
222-333-4444
222 - 333 - 4444
I have made this , but it accepts many other strings
^(.*?[.-|( |]|[0-9][^d]*(d+)[^d].* [.-|)].*)$
I am using C#.
c# regex
2
I don't see much logic behind the regex given here. Nor do I see any effort on your part to solve it.
– Tim Biegeleisen
Nov 11 at 10:47
You should look have looked at regex101.com and reviewed regex videos on Youtube. You've not even disclosed the language you are working on - there are some subtle nuances that are not in all variants. stackoverflow.com/help/how-to-ask We're not here to write for you - but to offer guidance..
– JGFMK
Nov 11 at 10:51
^(?=D*(?:dD*){10,11}$)d*(?:s*(?:-s*|(d+)))?s*d+(?:s*-?s*d+)*$
?
– Wiktor Stribiżew
Nov 11 at 11:12
add a comment |
up vote
-1
down vote
favorite
up vote
-1
down vote
favorite
I need a reg ex which accepts 10 to 11 digits phone number, hyphen , parenthesis (in right order) and spaces are allowed (I will remove all of the afterwards).
So all following string should be accepted by it.
12223334444
2223334444
1(222)333-4444
1(222) 333-4444
1 (222) 333-4444
1 (222) 333 - 4444
(222)333-4444
(222) 333-4444
(222) 333 - 4444
1-222-333-4444
1 - 222 - 333 - 4444
222-333-4444
222 - 333 - 4444
I have made this , but it accepts many other strings
^(.*?[.-|( |]|[0-9][^d]*(d+)[^d].* [.-|)].*)$
I am using C#.
c# regex
I need a reg ex which accepts 10 to 11 digits phone number, hyphen , parenthesis (in right order) and spaces are allowed (I will remove all of the afterwards).
So all following string should be accepted by it.
12223334444
2223334444
1(222)333-4444
1(222) 333-4444
1 (222) 333-4444
1 (222) 333 - 4444
(222)333-4444
(222) 333-4444
(222) 333 - 4444
1-222-333-4444
1 - 222 - 333 - 4444
222-333-4444
222 - 333 - 4444
I have made this , but it accepts many other strings
^(.*?[.-|( |]|[0-9][^d]*(d+)[^d].* [.-|)].*)$
I am using C#.
c# regex
c# regex
edited Nov 11 at 11:03
melpomene
56.8k54488
56.8k54488
asked Nov 11 at 10:43
User1162527
82110
82110
2
I don't see much logic behind the regex given here. Nor do I see any effort on your part to solve it.
– Tim Biegeleisen
Nov 11 at 10:47
You should look have looked at regex101.com and reviewed regex videos on Youtube. You've not even disclosed the language you are working on - there are some subtle nuances that are not in all variants. stackoverflow.com/help/how-to-ask We're not here to write for you - but to offer guidance..
– JGFMK
Nov 11 at 10:51
^(?=D*(?:dD*){10,11}$)d*(?:s*(?:-s*|(d+)))?s*d+(?:s*-?s*d+)*$
?
– Wiktor Stribiżew
Nov 11 at 11:12
add a comment |
2
I don't see much logic behind the regex given here. Nor do I see any effort on your part to solve it.
– Tim Biegeleisen
Nov 11 at 10:47
You should look have looked at regex101.com and reviewed regex videos on Youtube. You've not even disclosed the language you are working on - there are some subtle nuances that are not in all variants. stackoverflow.com/help/how-to-ask We're not here to write for you - but to offer guidance..
– JGFMK
Nov 11 at 10:51
^(?=D*(?:dD*){10,11}$)d*(?:s*(?:-s*|(d+)))?s*d+(?:s*-?s*d+)*$
?
– Wiktor Stribiżew
Nov 11 at 11:12
2
2
I don't see much logic behind the regex given here. Nor do I see any effort on your part to solve it.
– Tim Biegeleisen
Nov 11 at 10:47
I don't see much logic behind the regex given here. Nor do I see any effort on your part to solve it.
– Tim Biegeleisen
Nov 11 at 10:47
You should look have looked at regex101.com and reviewed regex videos on Youtube. You've not even disclosed the language you are working on - there are some subtle nuances that are not in all variants. stackoverflow.com/help/how-to-ask We're not here to write for you - but to offer guidance..
– JGFMK
Nov 11 at 10:51
You should look have looked at regex101.com and reviewed regex videos on Youtube. You've not even disclosed the language you are working on - there are some subtle nuances that are not in all variants. stackoverflow.com/help/how-to-ask We're not here to write for you - but to offer guidance..
– JGFMK
Nov 11 at 10:51
^(?=D*(?:dD*){10,11}$)d*(?:s*(?:-s*|(d+)))?s*d+(?:s*-?s*d+)*$
?– Wiktor Stribiżew
Nov 11 at 11:12
^(?=D*(?:dD*){10,11}$)d*(?:s*(?:-s*|(d+)))?s*d+(?:s*-?s*d+)*$
?– Wiktor Stribiżew
Nov 11 at 11:12
add a comment |
1 Answer
1
active
oldest
votes
up vote
1
down vote
accepted
Try this:
^(?:1[s-]*)?(?:(d{3})|d{3})[s-]*d{3}[s-]*d{4}$
See it here
Breakdown:
^
start of string
1[s-]*
(optional) Matches a literal1
and zero or more whitespace characters and/or dashes
(?:(d{3})|d{3})
matches one of the following:
- 3 digits
d{3}
surrounded by opening(
and closing)
parenthesis - 3 digits
d{3}
not surrounded by parenthesis
- 3 digits
[s-]*
zero or more whitespace characters and/or dashes
d{3}
another 3 digits
[s-]*
zero or more whitespace characters and/or dashes
d{4}
another 4 digits
$
end of string
Thank you for the answer and the breakdown, helps a lot.
– User1162527
Nov 11 at 11:29
Happy to assist
– K.Dᴀᴠɪs
Nov 11 at 11:30
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
1
down vote
accepted
Try this:
^(?:1[s-]*)?(?:(d{3})|d{3})[s-]*d{3}[s-]*d{4}$
See it here
Breakdown:
^
start of string
1[s-]*
(optional) Matches a literal1
and zero or more whitespace characters and/or dashes
(?:(d{3})|d{3})
matches one of the following:
- 3 digits
d{3}
surrounded by opening(
and closing)
parenthesis - 3 digits
d{3}
not surrounded by parenthesis
- 3 digits
[s-]*
zero or more whitespace characters and/or dashes
d{3}
another 3 digits
[s-]*
zero or more whitespace characters and/or dashes
d{4}
another 4 digits
$
end of string
Thank you for the answer and the breakdown, helps a lot.
– User1162527
Nov 11 at 11:29
Happy to assist
– K.Dᴀᴠɪs
Nov 11 at 11:30
add a comment |
up vote
1
down vote
accepted
Try this:
^(?:1[s-]*)?(?:(d{3})|d{3})[s-]*d{3}[s-]*d{4}$
See it here
Breakdown:
^
start of string
1[s-]*
(optional) Matches a literal1
and zero or more whitespace characters and/or dashes
(?:(d{3})|d{3})
matches one of the following:
- 3 digits
d{3}
surrounded by opening(
and closing)
parenthesis - 3 digits
d{3}
not surrounded by parenthesis
- 3 digits
[s-]*
zero or more whitespace characters and/or dashes
d{3}
another 3 digits
[s-]*
zero or more whitespace characters and/or dashes
d{4}
another 4 digits
$
end of string
Thank you for the answer and the breakdown, helps a lot.
– User1162527
Nov 11 at 11:29
Happy to assist
– K.Dᴀᴠɪs
Nov 11 at 11:30
add a comment |
up vote
1
down vote
accepted
up vote
1
down vote
accepted
Try this:
^(?:1[s-]*)?(?:(d{3})|d{3})[s-]*d{3}[s-]*d{4}$
See it here
Breakdown:
^
start of string
1[s-]*
(optional) Matches a literal1
and zero or more whitespace characters and/or dashes
(?:(d{3})|d{3})
matches one of the following:
- 3 digits
d{3}
surrounded by opening(
and closing)
parenthesis - 3 digits
d{3}
not surrounded by parenthesis
- 3 digits
[s-]*
zero or more whitespace characters and/or dashes
d{3}
another 3 digits
[s-]*
zero or more whitespace characters and/or dashes
d{4}
another 4 digits
$
end of string
Try this:
^(?:1[s-]*)?(?:(d{3})|d{3})[s-]*d{3}[s-]*d{4}$
See it here
Breakdown:
^
start of string
1[s-]*
(optional) Matches a literal1
and zero or more whitespace characters and/or dashes
(?:(d{3})|d{3})
matches one of the following:
- 3 digits
d{3}
surrounded by opening(
and closing)
parenthesis - 3 digits
d{3}
not surrounded by parenthesis
- 3 digits
[s-]*
zero or more whitespace characters and/or dashes
d{3}
another 3 digits
[s-]*
zero or more whitespace characters and/or dashes
d{4}
another 4 digits
$
end of string
edited Nov 11 at 11:14
answered Nov 11 at 11:04
K.Dᴀᴠɪs
6,076112140
6,076112140
Thank you for the answer and the breakdown, helps a lot.
– User1162527
Nov 11 at 11:29
Happy to assist
– K.Dᴀᴠɪs
Nov 11 at 11:30
add a comment |
Thank you for the answer and the breakdown, helps a lot.
– User1162527
Nov 11 at 11:29
Happy to assist
– K.Dᴀᴠɪs
Nov 11 at 11:30
Thank you for the answer and the breakdown, helps a lot.
– User1162527
Nov 11 at 11:29
Thank you for the answer and the breakdown, helps a lot.
– User1162527
Nov 11 at 11:29
Happy to assist
– K.Dᴀᴠɪs
Nov 11 at 11:30
Happy to assist
– K.Dᴀᴠɪs
Nov 11 at 11:30
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%2f53247950%2freg-ex-to-accept-digits-hyphen-and-parenthesis-in-right-order%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
2
I don't see much logic behind the regex given here. Nor do I see any effort on your part to solve it.
– Tim Biegeleisen
Nov 11 at 10:47
You should look have looked at regex101.com and reviewed regex videos on Youtube. You've not even disclosed the language you are working on - there are some subtle nuances that are not in all variants. stackoverflow.com/help/how-to-ask We're not here to write for you - but to offer guidance..
– JGFMK
Nov 11 at 10:51
^(?=D*(?:dD*){10,11}$)d*(?:s*(?:-s*|(d+)))?s*d+(?:s*-?s*d+)*$
?– Wiktor Stribiżew
Nov 11 at 11:12