Regex: Detect Phone numbers that are separated by dashes (-) and/or spaces
I am trying to recognize these types of phone number inputs:
- 0172665476
- +6265476393
- +62-65476393
- +62-654-76393
- +62 65476393
While my regex: (?:d+s*)+
can recognize the 1st 2 sample values, it recognizes the last 3 sample values as multiple matches in each line, instead of recognizing the number as a whole.
How can I modify this to support multiple dashes and/or spaces and still recognize it as 1 whole number instead of multiple matches?
regex
add a comment |
I am trying to recognize these types of phone number inputs:
- 0172665476
- +6265476393
- +62-65476393
- +62-654-76393
- +62 65476393
While my regex: (?:d+s*)+
can recognize the 1st 2 sample values, it recognizes the last 3 sample values as multiple matches in each line, instead of recognizing the number as a whole.
How can I modify this to support multiple dashes and/or spaces and still recognize it as 1 whole number instead of multiple matches?
regex
@Delgan Close! it includes a trailing space if there's any space after the number: "Hi my number is 0172665476 okay?"
– Dinuka
Jan 2 at 16:40
check this regex101.com/r/wxIiPs/2
– Code Maniac
Jan 2 at 16:44
add a comment |
I am trying to recognize these types of phone number inputs:
- 0172665476
- +6265476393
- +62-65476393
- +62-654-76393
- +62 65476393
While my regex: (?:d+s*)+
can recognize the 1st 2 sample values, it recognizes the last 3 sample values as multiple matches in each line, instead of recognizing the number as a whole.
How can I modify this to support multiple dashes and/or spaces and still recognize it as 1 whole number instead of multiple matches?
regex
I am trying to recognize these types of phone number inputs:
- 0172665476
- +6265476393
- +62-65476393
- +62-654-76393
- +62 65476393
While my regex: (?:d+s*)+
can recognize the 1st 2 sample values, it recognizes the last 3 sample values as multiple matches in each line, instead of recognizing the number as a whole.
How can I modify this to support multiple dashes and/or spaces and still recognize it as 1 whole number instead of multiple matches?
regex
regex
asked Jan 2 at 16:31


DinukaDinuka
1,88833580
1,88833580
@Delgan Close! it includes a trailing space if there's any space after the number: "Hi my number is 0172665476 okay?"
– Dinuka
Jan 2 at 16:40
check this regex101.com/r/wxIiPs/2
– Code Maniac
Jan 2 at 16:44
add a comment |
@Delgan Close! it includes a trailing space if there's any space after the number: "Hi my number is 0172665476 okay?"
– Dinuka
Jan 2 at 16:40
check this regex101.com/r/wxIiPs/2
– Code Maniac
Jan 2 at 16:44
@Delgan Close! it includes a trailing space if there's any space after the number: "Hi my number is 0172665476 okay?"
– Dinuka
Jan 2 at 16:40
@Delgan Close! it includes a trailing space if there's any space after the number: "Hi my number is 0172665476 okay?"
– Dinuka
Jan 2 at 16:40
check this regex101.com/r/wxIiPs/2
– Code Maniac
Jan 2 at 16:44
check this regex101.com/r/wxIiPs/2
– Code Maniac
Jan 2 at 16:44
add a comment |
4 Answers
4
active
oldest
votes
You may use this regex:
^+?d+(?:[s-]d+)*b
RegEx Details:
^+?
: Match optional+
at start
d+
: match 1+ digits
(?:[s-]d+)*
: Match 0 or more groups that start with whitespace or-
followed by 1+ digits
$
: End (Replaced by word boundary as if there are trailing spaces, that match would be missed.)
add a comment |
This should work:
(?:[d +-]+)+
1
This matches all spaces as well: regex101.com/r/IoWOhp/2
– Dinuka
Jan 2 at 16:38
add a comment |
This would work as per your reqt: (If there are trailing spaces, this regex will ignore.)
Regex: '^(?:[d +-]+)b'
add a comment |
Another option could be to use an alternation to match either 10 digits without a leading plus sign or match the pattern with a +
, and optional space or hyphen:
(?:d{10}|+d{2}[- ]?d{3}-?d{5})b
That will match:
(?:
Non capturing group
d{10}
Match 10 digits
|
Or
+d{2}[-s]?d{3}-?d{5}
Match+
, 2 digits, optional whitespace char or-
, 3 digits, optional-
, 5 digits
)b
Close non capturing group and word boundary
Regex demo
If your language supports negative lookbehinds you could prepend (?<!S)
which checks that what comes before is not a non-whitespace character.
add a comment |
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',
autoActivateHeartbeat: false,
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
});
}
});
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%2f54009879%2fregex-detect-phone-numbers-that-are-separated-by-dashes-and-or-spaces%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
4 Answers
4
active
oldest
votes
4 Answers
4
active
oldest
votes
active
oldest
votes
active
oldest
votes
You may use this regex:
^+?d+(?:[s-]d+)*b
RegEx Details:
^+?
: Match optional+
at start
d+
: match 1+ digits
(?:[s-]d+)*
: Match 0 or more groups that start with whitespace or-
followed by 1+ digits
$
: End (Replaced by word boundary as if there are trailing spaces, that match would be missed.)
add a comment |
You may use this regex:
^+?d+(?:[s-]d+)*b
RegEx Details:
^+?
: Match optional+
at start
d+
: match 1+ digits
(?:[s-]d+)*
: Match 0 or more groups that start with whitespace or-
followed by 1+ digits
$
: End (Replaced by word boundary as if there are trailing spaces, that match would be missed.)
add a comment |
You may use this regex:
^+?d+(?:[s-]d+)*b
RegEx Details:
^+?
: Match optional+
at start
d+
: match 1+ digits
(?:[s-]d+)*
: Match 0 or more groups that start with whitespace or-
followed by 1+ digits
$
: End (Replaced by word boundary as if there are trailing spaces, that match would be missed.)
You may use this regex:
^+?d+(?:[s-]d+)*b
RegEx Details:
^+?
: Match optional+
at start
d+
: match 1+ digits
(?:[s-]d+)*
: Match 0 or more groups that start with whitespace or-
followed by 1+ digits
$
: End (Replaced by word boundary as if there are trailing spaces, that match would be missed.)
edited Jan 3 at 15:07
Deep
193211
193211
answered Jan 2 at 16:51
anubhavaanubhava
533k48331408
533k48331408
add a comment |
add a comment |
This should work:
(?:[d +-]+)+
1
This matches all spaces as well: regex101.com/r/IoWOhp/2
– Dinuka
Jan 2 at 16:38
add a comment |
This should work:
(?:[d +-]+)+
1
This matches all spaces as well: regex101.com/r/IoWOhp/2
– Dinuka
Jan 2 at 16:38
add a comment |
This should work:
(?:[d +-]+)+
This should work:
(?:[d +-]+)+
answered Jan 2 at 16:36
feliksfeliks
1,070316
1,070316
1
This matches all spaces as well: regex101.com/r/IoWOhp/2
– Dinuka
Jan 2 at 16:38
add a comment |
1
This matches all spaces as well: regex101.com/r/IoWOhp/2
– Dinuka
Jan 2 at 16:38
1
1
This matches all spaces as well: regex101.com/r/IoWOhp/2
– Dinuka
Jan 2 at 16:38
This matches all spaces as well: regex101.com/r/IoWOhp/2
– Dinuka
Jan 2 at 16:38
add a comment |
This would work as per your reqt: (If there are trailing spaces, this regex will ignore.)
Regex: '^(?:[d +-]+)b'
add a comment |
This would work as per your reqt: (If there are trailing spaces, this regex will ignore.)
Regex: '^(?:[d +-]+)b'
add a comment |
This would work as per your reqt: (If there are trailing spaces, this regex will ignore.)
Regex: '^(?:[d +-]+)b'
This would work as per your reqt: (If there are trailing spaces, this regex will ignore.)
Regex: '^(?:[d +-]+)b'
edited Jan 2 at 18:11
answered Jan 2 at 17:08
DeepDeep
193211
193211
add a comment |
add a comment |
Another option could be to use an alternation to match either 10 digits without a leading plus sign or match the pattern with a +
, and optional space or hyphen:
(?:d{10}|+d{2}[- ]?d{3}-?d{5})b
That will match:
(?:
Non capturing group
d{10}
Match 10 digits
|
Or
+d{2}[-s]?d{3}-?d{5}
Match+
, 2 digits, optional whitespace char or-
, 3 digits, optional-
, 5 digits
)b
Close non capturing group and word boundary
Regex demo
If your language supports negative lookbehinds you could prepend (?<!S)
which checks that what comes before is not a non-whitespace character.
add a comment |
Another option could be to use an alternation to match either 10 digits without a leading plus sign or match the pattern with a +
, and optional space or hyphen:
(?:d{10}|+d{2}[- ]?d{3}-?d{5})b
That will match:
(?:
Non capturing group
d{10}
Match 10 digits
|
Or
+d{2}[-s]?d{3}-?d{5}
Match+
, 2 digits, optional whitespace char or-
, 3 digits, optional-
, 5 digits
)b
Close non capturing group and word boundary
Regex demo
If your language supports negative lookbehinds you could prepend (?<!S)
which checks that what comes before is not a non-whitespace character.
add a comment |
Another option could be to use an alternation to match either 10 digits without a leading plus sign or match the pattern with a +
, and optional space or hyphen:
(?:d{10}|+d{2}[- ]?d{3}-?d{5})b
That will match:
(?:
Non capturing group
d{10}
Match 10 digits
|
Or
+d{2}[-s]?d{3}-?d{5}
Match+
, 2 digits, optional whitespace char or-
, 3 digits, optional-
, 5 digits
)b
Close non capturing group and word boundary
Regex demo
If your language supports negative lookbehinds you could prepend (?<!S)
which checks that what comes before is not a non-whitespace character.
Another option could be to use an alternation to match either 10 digits without a leading plus sign or match the pattern with a +
, and optional space or hyphen:
(?:d{10}|+d{2}[- ]?d{3}-?d{5})b
That will match:
(?:
Non capturing group
d{10}
Match 10 digits
|
Or
+d{2}[-s]?d{3}-?d{5}
Match+
, 2 digits, optional whitespace char or-
, 3 digits, optional-
, 5 digits
)b
Close non capturing group and word boundary
Regex demo
If your language supports negative lookbehinds you could prepend (?<!S)
which checks that what comes before is not a non-whitespace character.
edited Jan 2 at 19:51
answered Jan 2 at 18:37
The fourth birdThe fourth bird
24.5k81629
24.5k81629
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.
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%2f54009879%2fregex-detect-phone-numbers-that-are-separated-by-dashes-and-or-spaces%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
@Delgan Close! it includes a trailing space if there's any space after the number: "Hi my number is 0172665476 okay?"
– Dinuka
Jan 2 at 16:40
check this regex101.com/r/wxIiPs/2
– Code Maniac
Jan 2 at 16:44