正则表达式的应用

1.拆分字符串
1.1 以下例举一个拆分句子的demo:
using System;
using System.Text.RegularExpressions;
namespace RegexSplit
{
class Program
{
public static void Main( string [] args)
{
Console . WriteLine( "请输入要分拆的字符串,并按Enter键确认。");
string input = Console . ReadLine();
string pattern = @"\.|,|:|;|。|,|:|;";
string [] rs = Regex . Split( input , pattern);
Console . WriteLine( "拆分后的所包含的分句为:");
foreach ( string s in rs)
{
Console . WriteLine(s);
}
Console . ReadKey( true);
}
}
}
1.2 拆分HTML标签
using System;
using System.Text.RegularExpressions;
namespace RegexSplit
{
class Program
{
public static void Main( string [] args)
{
string input = "<b>这是</b><b>一个</b><b>寂寞的天。</b><b>下着</b><b>有点</b><b>伤心地</b><b>雨。</b>";
string pattern = "<[^>]*>";
Regex r = new Regex( pattern);
string [] rs = r . Split( input);
Console . WriteLine( "拆分后的所包含的分句为:");
foreach ( string s in rs)
{
Console . WriteLine(s);
}
Console . ReadKey( true);
}
}
}

2.查询字符串
Regex类提供了三个方法来实现字符串的查询和匹配,Match,Matchs和IsMatch.
2.1 Match在指定的输入字符串中搜索Regex构造函数中指定的正则表达式,返回一个Match类对象,如果Match类对象的Success属性为true,则存在匹配的字符串,这个在前面的博文里已经说明了,可以使用NextMatch进行下一次匹配。
下面的例子查找HTML片段中所有带引号的URL
using System;
using System.Text.RegularExpressions;
namespace RegexSplit
{
class Test
{
static void PrintURL( string s)
{
string pattern = "href\\s*=\\s*\"[^\"]*\"";
Match m = Regex . Match(s , pattern);
while( m . Success)
{
Console . WriteLine( m . Value);
m = m . NextMatch();
}
}
public static void Main()
{
PrintURL( "href=\"http:\\\\www.baidu.com\" href=\"http:\\\\www.soso.com\"");
Console . ReadKey();
}
}
}

2.2  上面的例子也可以简化的返回一个MatchCollection集合,利用foreach遍历:
using System;
using System.Text.RegularExpressions;
namespace RegexSplit
{
class Test
{
static void PrintURL( string s)
{
string pattern = "href\\s*=\\s*\"[^\"]*\"";
MatchCollection m = Regex . Matches(s , pattern);
foreach( Match str in m)
{
Console . WriteLine( str);
}
}
public static void Main()
{
PrintURL( "href=\"http:\\\\www.baidu.com\" href=\"http:\\\\www.soso.com\"");
Console . ReadKey();
}
}
}   
假如我们仅仅想知道引用的地址可以使用捕获组及Match类的Groups属性。例如:
  using System;
using System.Text.RegularExpressions;
namespace RegexSplit
{
class Test
{
static void PrintURL( string s)
{
string pattern = "href\\s*=\\s*\"([^\"]*)\"";
MatchCollection m = Regex . Matches(s , pattern);
foreach( Match str in m)
{
Console . WriteLine( str . Groups [ 0 ] );
}
}
public static void Main()
{
PrintURL( "href=\"http:\\\\www.baidu.com\" href=\"http:\\\\www.soso.com\"");
Console . ReadKey();
}
}
}


2.3.1 IsMatch指示正则表达式再输入字符串中是否找到匹配项。
查找是否在字符创中包含<a>
using System;
using System.Text.RegularExpressions;
namespace RegexSplit
{
class Program
{
public static void Main( string [] args)
{
Regex r = new Regex ( "<a[^>]*>" , RegexOptions . IgnoreCase);
if( r . IsMatch( "<a href=\"http://www.baidu.com/\">链接</a>"))
Console . WriteLine( "包含<a>标签");
else
Console . WriteLine( "不包含<a>标签");
Console . ReadKey( true);
}
}
}
2.3.2用来验证输入16个数字
using System;
using System.Text.RegularExpressions;
namespace RegexSplit
{
class Program
{
public static void Main( string [] args)
{
Regex r = new Regex ( @"^\d{4}-\d{4}-\d{4}-\d{4}$");
if( r . IsMatch( "1216-2593-3395-2612"))
Console . WriteLine( "通过");
else
Console . WriteLine( "不通过");
Console . ReadKey( true);
}
}
}

3.替换字符串
Regex类的Replace方法用来替换字符串。下面的代码将输入字符串中的"China"都替换为“中国”。
using System;
using System.Text.RegularExpressions;
namespace RegexSplit
{
class Test
{
static void EtoC( string s)
{
Console . WriteLine( "源字符串:\n{0}" ,s);
Console . WriteLine( "替换后为:");
Console . WriteLine( Regex . Replace(s , "China" , "中国"));
}
public static void Main()
{
EtoC( "China啊我的祖国,China啊China!");
Console . ReadKey();
}
}
}

你可能感兴趣的:(正则表达式)