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#.










share|improve this question




















  • 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

















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#.










share|improve this question




















  • 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















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#.










share|improve this question















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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








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
















  • 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














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 literal 1 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




  • [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






share|improve this answer























  • 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











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















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

























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 literal 1 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




  • [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






share|improve this answer























  • 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















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 literal 1 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




  • [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






share|improve this answer























  • 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













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 literal 1 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




  • [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






share|improve this answer














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 literal 1 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




  • [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







share|improve this answer














share|improve this answer



share|improve this answer








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


















  • 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


















draft saved

draft discarded




















































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.




draft saved


draft discarded














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





















































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







Popular posts from this blog

Bressuire

Vorschmack

Quarantine