[英]Regex for phone # country code with area code
I need a regex to match this country-code + area code phone # format: 我需要一个正则表达式来匹配此国家代码+地区代码电话#格式:
1-201
where the first two characters are always 1-
and the last 3 characters are digits between 201
and 989
. 前两个字符始终为
1-
,后三个字符始终为201
到989
之间的数字。
I have ([1][\\-][0-9]{3})
currently to specify the 1-xyz
and limit length but how can I have the last group to restrict those ranges? 我目前有
([1][\\-][0-9]{3})
来指定1-xyz
和限制长度,但是我怎么能有最后一组来限制这些范围呢?
This will be used in PHP. 这将在PHP中使用。
Use this regex: 使用此正则表达式:
^1\-(2\d[1-9])|([3-8]\d{2})|(9[0-8]\d)$
Here is an explanation of the three capturing groups/ranges: 这是对三个捕获组/范围的说明:
(2\\d[1-9])
matches 201
to 299
(2\\d[1-9])
匹配201
至299
([3-8]\\d{2})
matches 300
to 899
([3-8]\\d{2})
匹配300
到899
(9[0-8]\\d)
matches 900
to 989
(9[0-8]\\d)
匹配900
到989
Here is a link where you can test this regex: 这是一个可以测试此正则表达式的链接:
Update: 更新:
Apparently Laravel doesn't like having so many nested capture groups, but this simplification should work for your needs: 显然,Laravel不喜欢拥有这么多嵌套的捕获组,但是这种简化应该可以满足您的需求:
1-(2\d[1-9]|[3-8]\d{2}|9[0-8]\d)
I would not use a regex for this. 我不会为此使用正则表达式。 It is going to be messy and hard to maintain.
它将变得凌乱并且难以维护。
I would do something like this: 我会做这样的事情:
$strings = array('1-201', '1-298', '1-989', '1-999', '1-200');
foreach($strings as $string) {
$value = explode('1-', $string);
if($value[1] >= 201 & $value[1] <= 989) {
echo 'In range' . $string . "\n";
} else {
echo 'out of range' . $string . "\n";
}
}
Output: 输出:
In range1-201
In range1-298
In range1-989
out of range1-999
out of range1-200
This should work, 这应该工作,
1-(20[1-9]|2[1-9][0-9]|[3-8][0-9][0-9]|9[0-8][0-9])
Alternately, 交替,
1-(20[1-9]|2[1-9]\d|[3-8]\d{2}|9[0-8]\d)
source: http://www.regular-expressions.info/numericranges.html 来源: http : //www.regular-expressions.info/numericranges.html
I think I would do it like the following in C#. 我想我会像在C#中一样执行以下操作。 Experimented.
实验过
string tester = "1-201";
Match match = Regex.Match(tester, @"(?<one>1-)(?<Two>[0-9]{3})");
//MessageBox.Show(match.Groups[2].Value);
int x = Convert.ToInt32(match.Groups[2].Value);
if (x <= 201 && x > 989)
{
//Exclude those captures not necessary.
//Use the captures within the range.
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.