• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

RegularExpressioninC#

原作者: [db:作者] 来自: [db:来源] 收藏 邀请
Essential Code to Use Regular Expression in C#

 

using System;

using System.Text.RegularExpressions;

 

           Regex phoneExp = new Regex(@"^\(\d{3}\)\s\d{3}-\d{4}$");

            string input;

            Console.Write("Enter a phone number: ");

            input = Console.ReadLine();

            while (phoneExp.Match(input).Success == false)
            {

                Console.WriteLine("Invalid input. Try again.");

                Console.Write("Enter a phone number: ");

                input = Console.ReadLine();
            }

            Console.WriteLine("Validated!");
   

[2]里还介绍了使用RE来进行replace和遍历匹配的字符串的代码.


 附

Regular Expression Single Character Classes

Class

Description

\d

Any digit

\D

Any nondigit

\w

Any word character

\W

Any nonword character

\s

Any whitespace character

\SW

Any nonwhitespace

 


Ranged and Set Character Classes

Format

Description

.

Any character except newline.

\p{uc}

Any character within the Unicode character category uc.

[abcxyz]

Any literal character in the set.

\P{uc}

Any character not within the Unicode character category uc.

[^abcxyz]

Any character not in the set of literal characters.

 


Character Class Quantifiers

Format

Description

*

0 or more characters

+

1 or more characters

?

0 or 1 characters

{n}

Exactly n characters

{n,}

At least n characters

{n,m}

At least n but no more than m characters

 


Positional (Atomic Zero-Width) Assertions

Format

Description

^

Beginning of a string or beginning of a newline

\z

End of the string, including the newline character

$

End of a string before a newline character or at the end of the line

\G

Continues where the last match left off

\A

Beginning of a string

\b

Between word boundaries (between alphanumeric and nonalphanumeric characters)

\Z

End of the string before the newline character

\B

Characters not between word boundaries

 


Reference
[1] http://www.codeproject.com/dotnet/regextutorial.asp
The 30 Minute Regex Tutorial (from codeproject) (
用实例介绍书写正则表达式的技巧,并提供了一个非常好的工具Expresso(甚至可以帮你分析你写的正则表达式))

[2] http://www.codeproject.com/books/0672325802.asp

Strings and Regular Expressions (chapter 3 of Microsoft Visual C# .NET 2003 Developer's Cookbook) (C#RE的基本知识,基本的使用代码)

[3] http://msdn2.microsoft.com/en-us/library/system.text.regularexpressions.aspx

System.Text.RegularExpressions Namespace (.net framework中正则表达式有关的类,Regex, Match,RegexOptions)

[4] http://www.ultrapico.com/

download the latest Expresso (an easy tool to learn regular expression)

 

[5] http://msdn2.microsoft.com/en-US/library/hs600312.aspx

.NET Framework Regular Expressions(有很多实用的例子)


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
C#解析Xml格式的字符串发布时间:2022-07-13
下一篇:
C#计算两个日期之间的所有日期发布时间:2022-07-13
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap