[英]Simple phone number regex to match numbers, spaces, etc
I'm trying to modify a fairly basic regex pattern in C# that tests for phone numbers. 我正在尝试在C#中修改一个相当基本的正则表达式模式,以测试电话号码。
The patterns is - 模式是-
[0-9]+(\.[0-9][0-9]?)?
I have two questions - 我有两个问题-
1) The existing expression does work (although it is fairly restrictive) but I can't quite understand how it works. 1)现有的表达式确实可以工作(尽管它是限制性的),但是我不太理解它是如何工作的。 Regexps for similar issues seem to look more like this one -
解决类似问题的正则表达式看起来更像是这个-
/^[0-9()]+$/
2) How could I extend this pattern to allow brackets, periods and a single space to separate numbers. 2)如何扩展此模式以允许方括号,句号和单个空格分隔数字。 I tried a few variations to include -
我尝试了一些变化以包括-
[0-9().+\s?](\.[0-9][0-9]?)?
Although i can't seem to create a valid pattern. 虽然我似乎无法创建有效的模式。
Any help would be much appreciated. 任何帮助将非常感激。
Thanks, 谢谢,
The +
after the character set is a quantifier (meaning the preceeding character, character set or group is repeated) at least one, and unlimited number of times and it's greedy (matched the most possible). 字符集后的
+
至少是一个量词(表示前面的字符,字符集或组被重复),并且次数不限且贪婪(最匹配)。
Then [0-9().+\\s]+
will match any character in set one or more times. 然后
[0-9().+\\s]+
将匹配集合中的任何字符一次或多次。
[0-9]+(\.[0-9][0-9]?)?
First of all, I recommend checking out either regexr.com or regex101.com, so you yourself get an understanding of how regex works. 首先,我建议您查看regexr.com或regex101.com,这样您自己就可以了解regex的工作原理。 Both websites will give you a step-by-step explanation of what each symbol in the regex does.
这两个网站都会为您提供有关正则表达式中每个符号作用的逐步说明。
Now, one of the main things you have to understand is that regex has special characters . 现在,您必须了解的主要内容之一是regex具有特殊字符 。 This includes, among others, the following:
[]().-+*?\\^$
. 其中包括以下内容:
[]().-+*?\\^$
。 So, if you want your regex to match a literal .
因此,如果您希望您的正则表达式匹配文字
.
, for example, you would have to escape it, since it's a special character. ,例如,您必须对其进行转义,因为它是一个特殊字符。 To do so, either use
\\.
为此,请使用
\\.
or [.]
. 或
[.]
。 Backslashes serve to escape other characters, while []
means "match any one of the characters in this set". 反斜杠用于转义其他字符,而
[]
表示“匹配此集合中的任何一个字符”。 Some special characters don't have a special meaning inside these brackets and don't require escaping. 一些特殊字符在这些括号内没有特殊含义,并且不需要转义。
Therefore, the regex above will match any combination of digits of length 1 or more, followed by an optional suffix (foobar)?
因此,上面的正则表达式将匹配长度为1或更大的数字的任意组合,后跟可选的后缀
(foobar)?
, which has to be a dot, followed by one or two digits. ,必须是一个点,后跟一个或两个数字。 In fact, this regex seems more like it's supposed to match decimal numbers with up to two digits behind the dot - not phone numbers.
实际上,此正则表达式似乎更应该与小数点匹配,点号后面最多可以有两位数字,而不是电话号码。
/^[0-9()]+$/
What this does is pretty simple - match any combination of digits or round brackets that has the length 1 or greater. 这非常简单-匹配长度为1或更大的数字或圆括号的任何组合。
[0-9().+\s?](\.[0-9][0-9]?)?
What you're matching here is: 您在这里匹配的是:
A suitable regex for your purpose could be: 适合您的正则表达式可以是:
(\+\d{2})?((\(0\)\d{2,3})|\d{2,3})?\d+
Enter this in one of the websites mentioned above to understand how it works. 在上面提到的网站之一中输入此内容以了解其工作原理。 I modified it a little to also allow, for example +49 123 4567890. Also, for simplicity, I didn't include spaces - so when using this regex, you have to remove all the spaces in your input first.
我对其进行了一些修改,例如+49 1234567890。此外,为简单起见,我不包含空格-因此,使用此正则表达式时,必须首先删除输入中的所有空格。 In C#, that should be possible with
yourString.Replace(" ", "");
在C#中,可以使用
yourString.Replace(" ", "");
(simply replacing all spaces with nothing = deleting spaces) (简单地用空格替换所有空格=删除空格)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.