【www.bbyears.com--正则表达式】
1.拆分字符串
1.1 以下例举一个拆分句子的demo:
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="这是一个寂寞的天。下着有点伤心地雨。";
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.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.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.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.111cn.net" href="http:\\www.soso.com"");
Console.ReadKey();
}
}
}
--------------------------------------------------------------------------------
2.3.1 IsMatch指示正则表达式再输入字符串中是否找到匹配项。
查找是否在字符创中包含
using System.Text.RegularExpressions;
namespace RegexSplit
{
class Program
{
public static void Main(string[] args)
{
Regex r=new Regex ("]*>",RegexOptions.IgnoreCase);
if(r.IsMatch("链接"))
Console.WriteLine("包含标签");
else
Console.WriteLine("不包含标签");
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.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();
}
}
}
例如,下面的函数演示了如何使用正则表达式验证邮政编码:
代码如下 private void ValidateZipButton_Click(object sender, System.EventArgs e){
String ZipRegex = @"^d{5}$";
if(Regex.IsMatch(ZipTextBox.Text, ZipRegex))
{
ResultLabel.Text = "ZIP is valid!";
}
else
{
ResultLabel.Text = "ZIP is invalid!";
}
}
类似的,可以使用静态 Replace() 方法将匹配替换为特定字符串,如下所示:
String newText = Regex.Replace(inputString, pattern, replacementText);
最后,可以使用如下代码遍历输入字符串的匹配集合:
{
MatchCollection matches = Regex.Matches(SearchStringTextBox.Text,
MatchExpressionTextBox.Text);
MatchCountLabel.Text = matches.Count.ToString();
MatchesLabel.Text = "";
foreach(Match match in matches)
{
MatchesLabel.Text += "Found" + match.ToString() + " at
position " + match.Index + ".
";
}
}
通常,在您需要指定默认方式以外的方式时,需要实例化 Regex 类的实例。特别是在设置选项时。例如,要创建忽略大小写和模式空白区域的 Regex 实例,然后检索与该表达式匹配的集合,则应使用如下代码:
代码如下Regex re = new Regex(pattern,
RegexOptions.IgnoreCase | RegexOptions.IgnorePatternWhitespace);
MatchCollection mc = re.Matches(inputString);
http url验证
代码如下Public Function IsValidUrl(ByVal Url As String) As Boolean
Dim strRegex As String = "^(https?://)" _
& "?(([0-9a-z_!~*"().&=+$%-]+: )?[0-9a-z_!~*"().&=+$%-]+@)?" _
& "(([0-9]{1,3}.){3}[0-9]{1,3}" _
& "|" _
& "([0-9a-z_!~*"()-]+.)*" _
& "([0-9a-z][0-9a-z-]{0,61})?[0-9a-z]." _
& "[a-z]{2,6})" _
& "(:[0-9]{1,4})?" _
& "((/?)|" _
& "(/[0-9a-z_!~*"().;?:@&=+$,%#-]+)+/?)$"
Dim re As RegularExpressions.Regex = New RegularExpressions.Regex(strRegex)
MessageBox.Show("IP: " & Net.IPAddress.TryParse(Url, Nothing))
If re.IsMatch(Url) Then
Return True
Else
Return False
End If
End Function