开发者

Can i Extract the Phone Numbers from Text using C#.net?

Can i Extract 开发者_如何学运维all the Phone Numbers from Text using C#.net?


Regular Expressions should allow you to do this.

There are a few phone number examples given in https://web.archive.org/web/20120527171308/http://en.csharp-online.net/CSharp_Regular_Expression_Recipes%E2%80%94Using_Common_Patterns

To use a regular expression on some text, you can use:

var exp = new Regex(
    @"(\(?[0-9]{3}\)?)?\-?[0-9]{3}\-?[0-9]{4}", // North American example
    RegexOptions.IgnoreCase);

var text = "My text including phone numbers";
MatchCollection matchList = exp.Matches(text);

// now iterate over matchList.Matches


• Match a North American phone number with an optional area code and an optional - character to be used in the phone number and no extension:

^(\(?[0-9]{3}\)?)?\-?[0-9]{3}\-?[0-9]{4}$

• Match a phone number like before but allow an optional five-digit extension prefixed with either ext or extension:

^(\(?[0-9]{3}\)?)?\-?[0-9]{3}\-?[0-9]{4}(\s*ext(ension)?[0-9]{5})?$


You can use regular expression look at this site regex library

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜