I'm trying to create a regular expression that would validate a specific phone number with an 868 area code and particular format h thus I want these
8680000000
(868) 000 0000
868-000-0000
868 000-0000
to pass validation but these
8880000000
767-000-0000
868-0000000
(868)0000000
to fail validation.
So far I got:
^((\(){0,1}(868){1}(\)){0,1})+:?((((\ |\-)(\d{3})(\ |\-)):?\d{4})+|(\d{4,7}))
But
(868)0000000
is validating
I genuinely do not understand why everyone need to validate a phone number with strict spacing, bracketing, hyphenation, and god-knows-what-else requirements.
Strip out all unnecessary characters, validate what needs validating, move on with life.
$phone = '(868) 555-1234'; // or 868 555.1234 or +868x55-5-123-4 etc
$phone = preg_replace('/[^0-9]/', '', $phone); // 8685551234
if( substring($phone, 0, 3) == '868' ) {
// great success
}
Bonus round. Format your data for hu-man display:
$phone_display = sprintf('(%s) %s-%s',
substring($phone,0,3),
substring($phone,3,3),
substring($phone,6,4));
Actually this is somewhat hard-coded because you are having some specific requirements like validating 868-000-0000
but not 868-0000000
.
Regex is:
(^\(868\)\ \d{3}\ \d{4})|(^868\d{7})|(^868\-\d{3}\-\d{4})|(^868\ \d{3}\-\d{4})
Such regex
^(\(?868\)?(\s*|-)\d{3}(\s|-)\d{4}|868\d{7})$
Validate these
8680000000
(868) 000 0000
868-000-0000
868 000-0000
Not validate
8880000000
767-000-0000
868-0000000
(868)0000000
match[1]
contains number
DEMO and explanation of the regex
You can do this with some conditionals
# ^(\()?868(?(1)\)[ ]\d{3}[ ]\d{4}|(?:(?:(-)|([ ])|)\d{3}(?(2)\2|(?(3)[ -]))\d{4}))$
^ # BOS
( \( )? # (1), Form (xxx) xxx xxxx
868 # 868 Area Code
(?(1) # Need Form 1 ?
\)
[ ] \d{3} [ ] \d{4}
| # else
(?:
(?:
( - ) # (2), Form xxx-xxx-xxxx
| ( [ ] ) # (3), Form xxx xxx[ -]xxxx
| # Form xxxxxxxxxx
)
\d{3}
(?(2) # Need form 2 ?
\2
| # else
(?(3) [ -] ) # Need form 3 ?
)
\d{4}
)
)
$ # EOS
Passed:
8680000000
(868) 000 0000
868-000-0000
868 000-0000
Failed:
8880000000
767-000-0000
868-0000000
(868)0000000
You could write one regular expression that matches all the formats, but there is no rule that says you have to.
Matching each format separately makes your code quite a bit more readable:
function matchPhoneNumber($phoneNumber) {
$regex = [
'/^(?P<prefix>868)(?P<first>\d{3})(?P<second>\d{4})$/',
'/^(?P<prefix>868)-(?P<first>\d{3})-(?P<second>\d{4})$/',
'/^(?P<prefix>868)\s(?P<first>\d{3})-(?P<second>\d{4})$/',
'/^\((?P<prefix>868)\)\s(?P<first>\d{3})\s(?P<second>\d{4})$/',
];
foreach ($regex as $reg) {
if (preg_match($reg, $phoneNumber, $match) === 1) {
return $match;
}
}
return false;
}
Assuming PHP uses EREs like awk
or grep -E
does:
$ grep -E '^((868([0-9]{3}|[- ][0-9]{3}-))|(\(868\) [0-9]{3} ))[0-9]{4}$' file
8680000000
(868) 000 0000
868-000-0000
868 000-0000
or if you like \\d
instead of [0-9]
:
^((868(\d{3}|[- ]\d{3}-))|(\(868\) \d{3} ))\d{4}$
Anything briefer would match other phone number formats that you didn't specify as being valid and so I'm assuming are invalid.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.