using System;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;
namespace 字符串
{
class 正則表達式
{
public static void Main()
{
//正則表達式是用來簡化字符串操作的,一般正則所實現的功能,用方法也一樣可以,只是方法有可能就會變的很復雜!
//假定我們要在字符串中做一次純文件的搜索,不用任何的正則串。
string Text = @"This comprehensive compendium provides a boroad and thor
This comprehensive compendium provides a boroad and thor
This comprehensive compendium provides a boroad and thor
This comprehensive compendium provides a boroad and thor
This comprehensive compendium provides a boroad and thor";
string Pattern = "thor";
MatchCollection Matchs = Regex.Matches( Text , Pattern , RegexOptions.IgnoreCase );
foreach (Match i in Matchs)
{
Console.WriteLine( i.Value );
}
//例:我們要查看我們的單詞是不是以N開頭
string myText = "Naladdin";
Match mat = Regex.Match( myText , @"n" , RegexOptions.IgnoreCase );
Console.WriteLine(myText "是否以N開頭" mat.Success);
//以A開頭,以ion結尾
string myText2 = "Applaldafaslkdfasd;fasdfasdfasdfadsfadfadsfication";
Match mat2 = Regex.Match(myText2, @"aS*ion", RegexOptions.IgnoreCase);
Console.WriteLine(myText2 "是否以A開頭Ion結尾,任意長度的單詞" mat2.Success);
Console.ReadLine();
}
}
}