C#中的正则表达式用于电话号码 [英] Regular expression in C# for phone number

查看:329
本文介绍了C#中的正则表达式用于电话号码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有匹配电话号码的表达式:

i Have that expression for matching phone numbers:

Quote:

@(\ + [0 -9] {2} | + \ [0-9] {2} \(0\)| \(\ + [0-9] {2} \)\(0\) | 00 [0-9] {2} | 0)([0-9] {9} | [0-9 \-\s] {9,18});

@"(\+[0-9]{2}|\+[0-9]{2}\(0\)|\(\+[0-9]{2}\)\(0\)|00[0-9]{2}|0)([0-9]{9}|[0-9\-\s]{9,18})";







但我想从文本文件中识别德国电话号码:

格式为:0176/21425679

0157764123813

09621 11 85 45

0172 8622111

08103/30 444 30

+49 176 3123 8343



如何识别这些类型的电话号码。

我的表达工作但不完全正确。



我尝试了什么:






but i want to identify German phone number from text file:
formats are: 0176/21425679
0157764123813
09621 11 85 45
0172 8622111
08103 / 30 444 30
+49 176 3123 8343

how can identify these type of phone numbers.
my expression working but not exactly.

What I have tried:

<pre>@"(\+[0-9]{2}|\+[0-9]{2}\(0\)|\(\+[0-9]{2}\)\(0\)|00[0-9]{2}|0)([0-9]{9}|[0-9\-\s]{9,18})";







C#




C#

推荐答案

首先取决于你需要什么:

只需验证是否它是有效的(德国)电话号码或以定义的格式获取号码。



无论如何,将处理分成多个部分可能更好。



所以你可以先用一个'0'替换可选的国际前缀:

At first it depends on what you need:
Just verify if it is a valid (German) phone number or get the number in a defined format.

In any case it might be better to split the processing into multiple parts.

So you might for example replace the optional international prefix with a single '0' at first:
/^\+[0-9]{2}[^0-9]*/0/
# or
/^\+[0-9]{2}[ -/]*/0/

如果您需要单独获取区号,请查找下一个非数字字符。但这对所有输入都不起作用。



最后删除所有分隔/分隔符(空格,减号,斜线)。最后一个字符串应该只包含数字,并且可以检查min。最多允许的长度。

If you need to get the area code separately look for the next non-digit character. But that will not work for all inputs.

Finally remove all separation / delimiter characters (space, minus, slash). The final string should contain digits only and can be checked for min. and max. allowed length.


Quote:

如何识别这些类型的电话号码。

how can identify these type of phone numbers.



您永远无法知道某个号码是电话号码还是电话号码,具体取决于具体情况。

据我所知,德国电话号码有所不同比特数。

+49是德国的国际标题。

空格和破折号是人工分隔符,具体取决于用户,分组可以有所不同,使数字更容易记住。

我会尝试类似的东西:


You can never know if a number is a phone number or look like a phone number, it depend on context.
As fa as I know, German phone number comes with different number of digits.
+49 is the international header to Germany.
Spaces and dashes are artificial separators depending on user, and grouping can differ to make number easier to remember.
I would try something like:

((\+49[ /]*)?(\d[ /]*){10,11}\d)



以下是RegEx文档的链接:

perlre - perldoc.perl.org [ ^ ]

以下是帮助构建RegEx并调试它们的工具的链接:

。NET Regex Tester - Regex Storm [ ^ ]

Expresso正则表达式工具 [ ^ ]

RegExr:学习,构建,和测试RegEx [ ^ ]

此节目RegEx是一个很好的图表,它非常有助于理解RegEx的作用:

Debuggex:在线可视正则表达式测试器。 JavaScript,Python和PCRE。 [ ^ ]


Here is a link to RegEx documentation:
perlre - perldoc.perl.org[^]
Here is links to tools to help build RegEx and debug them:
.NET Regex Tester - Regex Storm[^]
Expresso Regular Expression Tool[^]
RegExr: Learn, Build, & Test RegEx[^]
This one show you the RegEx as a nice graph which is really helpful to understand what is doing a RegEx:
Debuggex: Online visual regex tester. JavaScript, Python, and PCRE.[^]


这篇关于C#中的正则表达式用于电话号码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆