Jquery中文网 www.jquerycn.cn
Jquery中文网 >  脚本编程  >  Asp.net  >  正文 asp.net一些常用字符串操作正则表达式

asp.net一些常用字符串操作正则表达式

发布时间:2018-09-07   编辑:www.jquerycn.cn
jquery中文网为您提供asp.net一些常用字符串操作正则表达式等资源,欢迎您收藏本站,我们将为您提供最新的asp.net一些常用字符串操作正则表达式资源
文章收集了一些常用的正则规则包括有拆分字符串 拆分HTML标签 查询字符串 查找是否在字符创中包含 用来验证输入16个数字等。

1.拆分字符串
1.1 以下例举一个拆分句子的demo:

<table width="620" align="center" border="0" cellpadding="1" cellspacing="1" style="background:#FB7"> <tr> <td width="464" height="27" bgcolor="#FFE7CE"> 代码如下</td> <td width="109" align="center" bgcolor="#FFE7CE" style="cursor:pointer;" onclick="doCopy('copy4784')">复制代码</td> </tr> <tr> <td height="auto" colspan="2" valign="top" bgcolor="#FFFFFF" style="padding:10px;" class="copyclass" id=copy4784>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标签

<table width="620" align="center" border="0" cellpadding="1" cellspacing="1" style="background:#FB7"> <tr> <td width="464" height="27" bgcolor="#FFE7CE"> 代码如下</td> <td width="109" align="center" bgcolor="#FFE7CE" style="cursor:pointer;" onclick="doCopy('copy7852')">复制代码</td> </tr> <tr> <td height="auto" colspan="2" valign="top" bgcolor="#FFFFFF" style="padding:10px;" class="copyclass" id=copy7852>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

<table width="620" align="center" border="0" cellpadding="1" cellspacing="1" style="background:#FB7"> <tr> <td width="464" height="27" bgcolor="#FFE7CE"> 代码如下</td> <td width="109" align="center" bgcolor="#FFE7CE" style="cursor:pointer;" onclick="doCopy('copy3264')">复制代码</td> </tr> <tr> <td height="auto" colspan="2" valign="top" bgcolor="#FFFFFF" style="padding:10px;" class="copyclass" id=copy3264>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遍历:

<table width="620" align="center" border="0" cellpadding="1" cellspacing="1" style="background:#FB7"> <tr> <td width="464" height="27" bgcolor="#FFE7CE"> 代码如下</td> <td width="109" align="center" bgcolor="#FFE7CE" style="cursor:pointer;" onclick="doCopy('copy9829')">复制代码</td> </tr> <tr> <td height="auto" colspan="2" valign="top" bgcolor="#FFFFFF" style="padding:10px;" class="copyclass" id=copy9829>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类的Grou正则表达式验证邮政编码:

<table width="620" align="center" border="0" cellpadding="1" cellspacing="1" style="background:#FB7"> <tr> <td width="464" height="27" bgcolor="#FFE7CE"> 代码如下</td> <td width="109" align="center" bgcolor="#FFE7CE" style="cursor:pointer;" onclick="doCopy('copy5169')">复制代码</td> </tr> <tr> <td height="auto" colspan="2" valign="top" bgcolor="#FFFFFF" style="padding:10px;" class="copyclass" id=copy5169>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);
最后,可以使用如下代码遍历输入字符串的匹配集合:

<table width="620" align="center" border="0" cellpadding="1" cellspacing="1" style="background:#FB7"> <tr> <td width="464" height="27" bgcolor="#FFE7CE"> 代码如下</td> <td width="109" align="center" bgcolor="#FFE7CE" style="cursor:pointer;" onclick="doCopy('copy2535')">复制代码</td> </tr> <tr> <td height="auto" colspan="2" valign="top" bgcolor="#FFFFFF" style="padding:10px;" class="copyclass" id=copy2535>private void MatchButton_Click(object sender, System.EventArgs e)
{
   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 ".<br>";
   }
}

通常,在您需要指定默认方式以外的方式时,需要实例化 Regex 类的实例。特别是在设置选项时。例如,要创建忽略大小写和模式空白区域的 Regex 实例,然后检索与该表达式匹配的集合,则应使用如下代码:

<table width="620" align="center" border="0" cellpadding="1" cellspacing="1" style="background:#FB7"> <tr> <td width="464" height="27" bgcolor="#FFE7CE"> 代码如下</td> <td width="109" align="center" bgcolor="#FFE7CE" style="cursor:pointer;" onclick="doCopy('copy9620')">复制代码</td> </tr> <tr> <td height="auto" colspan="2" valign="top" bgcolor="#FFFFFF" style="padding:10px;" class="copyclass" id=copy9620>

Regex re = new Regex(pattern,
   RegexOptions.IgnoreCase | RegexOptions.IgnorePatternWhitespace);
MatchCollection mc = re.Matches(inputString);

http url验证

<table width="620" align="center" border="0" cellpadding="1" cellspacing="1" style="background:#FB7"> <tr> <td width="464" height="27" bgcolor="#FFE7CE"> 代码如下</td> <td width="109" align="center" bgcolor="#FFE7CE" style="cursor:pointer;" onclick="doCopy('copy1575')">复制代码</td> </tr> <tr> <td height="auto" colspan="2" valign="top" bgcolor="#FFFFFF" style="padding:10px;" class="copyclass" id=copy1575>

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

您可能感兴趣的文章:
正则regex的posix标准,gnu扩展以及PC兼容性
正则表达式使用详解
MongoDB 正则表达式
正则表达式 模式匹配 Javascript
常用正则表达式全集
PHP中正则表达式模式修饰符详解
.net开发中常用正则表达式
php正则ereg ereg_replace eregi eregi_replace split
正则表达式使用详解
学习javascipt的正则表达式

[关闭]