正则表达式
正则表达式是一个用于匹配、提取、替换的包含元字符的字符串。 基本元字符. 任意的一个非换行字符
[] 集合匹配,表示匹配一个出现在[]中的一个字符
| 或:a|ab,敏感词:病毒|大麻
() 提高优先级实现分组,abc->a(bc)
限定元字符+ 紧跟在+前面的字符出现>=1次
* 紧跟在*前面的字符出现>=0次
? 紧跟在?前面的字符出现0或1次
{n} [0123456789]{11}表示数字出现11次
{n,} [5-9]{8,}表示5-9之间的数字出现>=8次,{1,}等价于+
{n,m}[0-9]{3,6}表示0-9之间的数字出现3-5次
w 匹配包括下划线在内的任意单词字符(包括汉字),约等价于[0-9a-zA-Z_]
W 匹配任何非单词字符,约等价于[^0-9a-zA-Z_]
s 匹配任何空白字符包括换页符、回车符、换行符、制表符、垂直制表符,等价于[frntv]
S 匹配任何非空白字符,等价于[^frntv]
d 匹配一个数字字符,等价于[0-9]
D 匹配一个非数字字符,等价于[^0-9]
匹配邮箱示例代码:static void Main(string[] args)
{
WebClient wc = new WebClient();
wc.Encoding = Encoding.UTF8;
string html = wc.DownloadString("http://tieba.baidu.com/p/2314539885");
//【正则表达式1】
//string regex = @"w+@w+(.w+)+";
//【正则表达式2(改进版)】
string regex = @"[0-9a-zA-Z_-.]+@[0-9a-zA-Z_]+(.[0-9a-zA-Z_]+)+";
MatchCollection mc = Regex.Matches(html,regex);
int i = 0;
foreach (Match match in mc)
{
Console.WriteLine(i++ + "," + match.Value);
}
Console.ReadKey();
}
示例正则表达式解析:示例邮箱地址:99999@china.com 正则表达式2:@”[0-9a-zA-Z_-.]+@[0-9a-zA-Z_]+(.[0-9a-zA-Z_]+)+” ^和$^once表示只匹配那些以”once”开头的字符串,^once[0-9][a-z]end表示只匹配以”once[0-9][a-z]end”开头的字符串。 public void TestStart()
{
int i = 0;
bool flag;
string input = "once1aend";
string regex = @"once[0-9][a-z]end";
string regexStart = @"^once[0-9][a-z]end";
Console.WriteLine("【"+input + "匹配" + regexStart + "的结果:】");
flag = Regex.IsMatch(input,regex);
Console.WriteLine("是否匹配=" + flag);
MatchCollection mc = Regex.Matches(input,regexStart);
foreach (Match match in mc)
{
Console.WriteLine(i++ + "," + match.Value);
}
Console.WriteLine("【" + input + "匹配" + regex + "的结果:】");
flag = Regex.IsMatch(input,regex);
Console.WriteLine("是否匹配=" + flag);
Console.WriteLine("匹配项:");
mc = Regex.Matches(input,regex);
i = 0;
foreach (Match match in mc)
{
Console.WriteLine(i++ + "," + match.Value);
}
Console.ReadKey();
}
运行结果如图:
once$表示只匹配那些以”once”结尾的字符串,once[0-9][a-z]end$表示只匹配以”once[0-9][a-z]end”结尾的字符串。 public void TestEnd()
{
int i = 0;
bool flag;
string input = "once1aend";
string regex = @"once[0-9][a-z]end";
string regexEnd = @"once[0-9][a-z]end$";
Console.WriteLine("【" + input + "匹配" + regexEnd + "的结果:】");
flag = Regex.IsMatch(input,regexEnd);
Console.WriteLine("是否匹配=" + flag);
Console.WriteLine("匹配项:");
//【改进】匹配^...或...$只有一个结果,所以用Match()而不是Matchs()
Match match = Regex.Match(input,regexEnd);
Console.WriteLine(i++ + "," + match.Value);
Console.WriteLine("---------------------------------");
Console.WriteLine("【" + input + "匹配" + regex + "的结果:】");
flag = Regex.IsMatch(input,regex);
Console.WriteLine("是否匹配=" + flag);
Console.WriteLine("匹配项:");
MatchCollection mc = Regex.Matches(input,regex);
i = 0;
foreach (Match obj in mc)
{
Console.WriteLine(i++ + "," + obj.Value);
}
}
运行结果如图:
^once$表示只匹配”once”字符串,^once[0-9][a-z]end$表示只匹配”once[0-9][a-z]end”字符串。 public void TestStratEnd()
{
int i = 0;
bool flag;
string input = "once1aend";
string regex = @"once[0-9][a-z]end";
string regexStartEnd = @"^once[0-9][a-z]end$";
Console.WriteLine("【" + input + "匹配" + regexStartEnd + "的结果:】");
flag = Regex.IsMatch(input,regexStartEnd);
Console.WriteLine("是否匹配=" + flag);
Console.WriteLine("匹配项:");
Match match = Regex.Match(input,regexStartEnd);
Console.WriteLine(i++ + "," + match.Value);
Console.WriteLine("--------------------------------");
Console.WriteLine("【" + input + "匹配" + regex + "的结果:】");
flag = Regex.IsMatch(input,regex);
Console.WriteLine("是否匹配=" + flag);
Console.WriteLine("匹配项:");
MatchCollection mc = Regex.Matches(input,regex);
i = 0;
foreach (Match obj in mc)
{
Console.WriteLine(i++ + "," + obj.Value);
}
}
运行结果如图:
匹配HTML标签使用"<html><body></body></html>"来匹配"<.+>",只能匹配到一个满足项,"html><body></body></html"都被匹配到.+中了,所以只有一个满足项。截图如下:
如果我们相匹配完整的HTML标签时,内容中不应该有">",改进后,使用<html><body></body></html>来匹配"<[^>]+>",能匹配到四个满足项,截图如下(实际中遇到类似情况知道怎么去改进就行了):
使用分组如果我们在正则提取过程中希望得到匹配项,同时需要使用匹配项中的某一部分的时候,使用分组可以方便的实现这个功能,只需要用圆括号把需要的部分括起来即可,例如:(.+)。
示例代码如下: public void TestGroup()
{
string regex = @"^(([0-9]+)([a-z]+))([0-9]+)$";
string input = "123abc456";
Match match = Regex.Match(input,regex);
for (int i = 0; i < match.Groups.Count; i++)
{
Console.WriteLine("match.Groups[" + i + "]=" +match.Groups[i].Value); } Console.ReadKey(); }
运行结果如图: 贪婪模式正则表达式默认匹配尽可能多,示例代码如下: public void TestGreed()
{
string input = "1234567890";
string regex = @"^(d+)(d+)(d+)$";
Match match = Regex.Match(input,regex);
for (int i = 0; i < match.Groups.Count; i++)
{
Console.WriteLine("match.Groups[" + i + "]=" +match.Groups[i].Value); } Console.ReadKey(); }
运行结果如图: 匹配一个整数整数分为:0、正整数、负整数,不允许出现0123这种形式 正则替换public void TestReplace()
{
string input = "ab------c---d---ef----g";
string result = Regex.Replace(input,@"-+","-");
Console.WriteLine("input=" + input);
Console.WriteLine("result=" + result);
}
运行结果如图: protected void Page_Load(object sender,EventArgs e)
{
if (!IsPostBack)
{
Label1.Text = "http://www.baidu.com123 4 5 6http://www.csdn.net/";
}
}
protected void btnReplace_Click(object sender,EventArgs e)
{
string input = "http://www.baidu.com123 4 5 6http://www.csdn.net/";
//不加()
string result = Regex.Replace(input,@"http://[w.]+.[a-z]+","<a href="$0">$0</a>");
//加上(),result==resultAdd
string resultAdd = Regex.Replace(input,@"(http://[w.]+.[a-z]+)","<a href="$1">$1</a>");
Label1.Text = result;
}
点击替换按钮之前: 注意一般,正则表达式很难一次就写得完美,我们可以先写出来测试一下,然后根据匹配到的结果,逐步精细化正则表达式以达到自己的目的。 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |