日本综合一区二区|亚洲中文天堂综合|日韩欧美自拍一区|男女精品天堂一区|欧美自拍第6页亚洲成人精品一区|亚洲黄色天堂一区二区成人|超碰91偷拍第一页|日韩av夜夜嗨中文字幕|久久蜜综合视频官网|精美人妻一区二区三区

RELATEED CONSULTING
相關(guān)咨詢
選擇下列產(chǎn)品馬上在線溝通
服務(wù)時(shí)間:8:30-17:00
你可能遇到了下面的問題
關(guān)閉右側(cè)工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
使用C#正則表達(dá)式匹配相關(guān)字符串

C#正則表達(dá)式匹配字符串的方法如下:

在金昌等地區(qū),都構(gòu)建了全面的區(qū)域性戰(zhàn)略布局,加強(qiáng)發(fā)展的系統(tǒng)性、市場前瞻性、產(chǎn)品創(chuàng)新能力,以專注、極致的服務(wù)理念,為客戶提供網(wǎng)站設(shè)計(jì)制作、做網(wǎng)站 網(wǎng)站設(shè)計(jì)制作按需制作網(wǎng)站,公司網(wǎng)站建設(shè),企業(yè)網(wǎng)站建設(shè),品牌網(wǎng)站制作,營銷型網(wǎng)站,成都外貿(mào)網(wǎng)站制作,金昌網(wǎng)站建設(shè)費(fèi)用合理。

1.使用C#中使用正則表達(dá)式System.Text.RegularExpressions命名空間;

2.使用C#中使用正則表達(dá)式Matches()方法匹配字符串,格式如下:
MatchCollection Matches = Regex.Matches(Str, Pattern, RegexOptions.IgnoreCase | RegexOptions.ExplicitCapture);

其中Str表示輸入字符串,Pattern表示匹配模式,RegexOptions.IgnoreCase表示忽略大小寫,RegexOptions.ExplicitCapture表示改變收集匹配方式。

3.匹配結(jié)果保存在MatchCollection集合中,可以通過循環(huán)遍歷結(jié)果集取出Match對(duì)象的結(jié)果。

TestRagular.cs:

 
 
 
  1. using System;
  2. using System.Text.RegularExpressions;
  3. namespace Magci.Test.Strings
  4. {
  5.     public class TestRegular
  6.     {
  7.         public static void WriteMatches(string str, MatchCollection matches)
  8.         {
  9.             Console.WriteLine("\nString is : " + str);
  10.             Console.WriteLine("No. of matches : " + matches.Count);
  11.             foreach (Match nextMatch in matches)
  12.             {
  13.                 //取出匹配字符串和最多10個(gè)外圍字符
  14.                 int Index = nextMatch.Index;
  15.                 string result = nextMatch.ToString();
  16.                 int charsBefore = (Index < 5) ? Index : 5;
  17.                 int fromEnd = str.Length - Index - result.Length;
  18.                 int charsAfter = (fromEnd < 5) ? fromEnd : 5;
  19.                 int charsToDisplay = charsBefore + result.Length + charsAfter;
  20.                 Console.WriteLine("Index: {0},\tString: {1},\t{2}", Index, result, str.Substring(Index - charsBefore, charsToDisplay));
  21.             }
  22.         }
  23.         public static void Main()
  24.         {
  25.             string Str = @"My name is Magci, for short mgc. I like c sharp!";
  26.             //查找“gc”
  27.             string Pattern = "gc";
  28.             MatchCollection Matches = Regex.Matches(Str, Pattern, RegexOptions.IgnoreCase | RegexOptions.ExplicitCapture);
  29.             WriteMatches(Str, Matches);
  30.             
  31.             //查找以“m”開頭,“c”結(jié)尾的單詞
  32.             Pattern = @"\bm\S*c\b";
  33.             Matches = Regex.Matches(Str, Pattern, RegexOptions.IgnoreCase | RegexOptions.ExplicitCapture);
  34.             WriteMatches(Str, Matches);
  35.         }
  36.     }
  37. }

名稱欄目:使用C#正則表達(dá)式匹配相關(guān)字符串
文章分享:http://www.dlmjj.cn/article/djgddgh.html