正則表達式(Regular expressions)是一套語法匹配規則,各類語言,如Perl, .Net和Java都有其git
對應的共享的正則表達式類庫。在.Net中,這個類庫叫作Regex。正則表達式
如下是Regex下的幾個靜態方法:express
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>");
}ui
執行的結果是:
First
Second
Thirdthis
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();
}spa
該事例運行結果是:
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=363d
如下是經過正則表達式查找在一個字符串中重複出現的字母的格式:code
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. |