码迷,mamicode.com
首页 > Windows程序 > 详细

4.C#学习之正则表达式&&Regex

时间:2015-04-19 15:55:53      阅读:217      评论:0      收藏:0      [点我收藏+]

标签:

  正则表达式(Regular expressions)是一套语法匹配规则,各种语言,如Perl, .Net和Java都有其

对应的共享的正则表达式类库。在.Net中,这个类库叫做Regex。

以下是Regex下的几个静态方法:

  Escape: 对字符串中的regex中的转义符进行转义; 
      IsMatch: 如果表达式在字符串中匹配,该方法返回一个布尔值; 
      Match: 返回Match的实例; 
      Matches: 返回一系列的Match的方法; 
      Replace: 用替换字符串替换匹配的表达式; 
      Split: 返回一系列由表达式决定的字符串; 
      Unescape:不对字符串中的转义字符转义。

以下代码示例创建了 Regex 类的实例并在初始化对象时定义一个简单的正则表达式。声明一个Regex对象变量:Regex objAlphaPatt;,接着创建Regex对象的一个实例,并定义其规则:objAlphaPatt=new Regex("[^a-zA-Z]");

IsMatch方法指示 Regex 构造函数中指定的正则表达式在输入字符串中是否找到匹配项。这是我们使用C#正则表达式时最常用的方法之一。下面的例子说明了IsMatch方法的使用:
if( !objAlphaPatt.IsMatch("testisMatchMethod"))
 lblMsg.Text = "匹配成功";
else
 lblMsg.Text = "匹配不成功";
这段代码执行的结果是“匹配成功”
if( ! objAlphaPatt.IsMatch("testisMatchMethod7654298"))
 lblMsg.Text = "匹配成功";
else
 lblMsg.Text = "匹配不成功";
这段代码执行的结果是“匹配不成功”

 

Split方法是把由正则表达式匹配项定义的位置将输入字符串拆分为一个子字符串数组。例如:
Regex r = new Regex("-"); // Split on hyphens.
string[] s = r.Split("first-second-third");
for(int i=0;i<s.Length;i++)
{
 Response.Write(s[i]+"<br>");
}

执行的结果是:
First
Second
Third

 

Match方法是在输入字符串中搜索正则表达式的匹配项,并Regex 类的 Match 方法返回 Match 对象,Match 类表示正则表达式匹配操作的结果。下面的例子演示Match方法的使用,并利用Match对象的Group属性返回Group对象:

string text = @"public string testMatchObj string s string  match ";
string pat = @"(\w+)\s+(string)";
// Compile the regular expression.
Regex r = new Regex(pat, RegexOptions.IgnoreCase);
// Match the regular expression pattern against a text string.
Match m = r.Match(text);
int matchCount = 0;
while (m.Success) 
{
 Response.Write("Match"+ (++matchCount) + "<br>");
 for (int i = 1; i <= 2; i++) 
 {
  Group g = m.Groups[i];
  Response.Write("Group"+i+"=‘" + g + "‘"  + "<br>");
  CaptureCollection cc = g.Captures;
  for (int j = 0; j < cc.Count; j++) 
  {
   Capture c = cc[j];
   Response.Write("Capture"+j+"=‘" + c + "‘, Position="+c.Index + "<br>");
  }
 }
 m = m.NextMatch();
}

该事例运行结果是:
Match1
Group1=‘public‘
Capture0=‘public‘, Position=0
Group2=‘string‘
Capture0=‘string‘, Position=7
Match2
Group1=‘testMatchObj‘
Capture0=‘testMatchObj‘, Position=14
Group2=‘string‘
Capture0=‘string‘, Position=27
Match3
Group1=‘s‘
Capture0=‘s‘, Position=34
Group2=‘string‘
Capture0=‘string‘, Position=36

 

以下是通过正则表达式查找在一个字符串中重复出现的字母的格式:

Pattern Description
\b Start the match at a word boundary.
(?<word>\w+) Match one or more word characters up to a word boundary. Name this captured group word.
\s+ Match one or more white-space characters.
(\k<word>) Match the captured group that is named word.
\b Match a word boundary.

以下是正则表达式的一些元字符:

Pattern

Description

^

Start at the beginning of the string.

\s*

Match zero or more white-space characters.

[\+-]?

Match zero or one occurrence of either the positive sign or the negative sign.

\s?

Match zero or one white-space character.

\$?

Match zero or one occurrence of the dollar sign.

\s?

Match zero or one white-space character.

\d*

Match zero or more decimal digits.

\.?

Match zero or one decimal point symbol.

\d{2}?

Match two decimal digits zero or one time.

(\d*\.?\d{2}?){1}

Match the pattern of integral and fractional digits separated by a decimal point symbol at least one time.

$

Match the end of the string.

4.C#学习之正则表达式&&Regex

标签:

原文地址:http://www.cnblogs.com/clownice/p/4439094.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!