正则表达式
匹配数字字母下划线: /^\w+$/g
例子:
text = Regex.Replace(text, "[\\s]{2,}", " "); //two or more spaces
text = Regex.Replace(text, "(<[b|B][r|R]/*>)+|(<[p|P](.|\\n)*?>)", "\n"); //<br>回车
text = Regex.Replace(text, "(\\s*&[n|N][b|B][s|S][p|P];\\s*)+", " "); // 空格
text = Regex.Replace(text, "<(.|\\n)*?>", string.Empty); //any other tags
text = text.Replace("'", "''");
前台验证:
邮政编码: reg=/^\d{6}$/g;
联系电话:reg=/^\d+$/gi;
电子邮箱:reg=/^\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/gi;
匹配所有(包括回车空格):((?s).*)
匹配所有(不包括回车):(.*) 贪婪模式 (.*?) 懒惰模式 在后面加个"?"
eg:
using (System.IO.StreamReader sr = new System.IO.StreamReader(@"E:\ZhongCJ\tvqun.com\WebSite\tvq\playdatas\231\2009\01\16\231-2009-01-16-126.php"))
{
string php = sr.ReadToEnd();
string context = "";
string strLast = php.Substring(php.LastIndexOf("';"));
php = php.Replace(strLast, "").Trim();
System.Text.RegularExpressions.Regex reg = new System.Text.RegularExpressions.Regex("echo '((?s).*)", System.Text.RegularExpressions.RegexOptions.IgnoreCase);
if (reg.IsMatch(php))
{
System.Text.RegularExpressions.MatchCollection groupList = reg.Matches(php);
context = groupList[0].Groups[1].Value.Trim();
}
Response.Write(context);
Response.End();
}
c# 得到匹配的集合
using StringDictionary = System.Collections.Specialized.StringDictionary;
using System.Collections.Generic;
using System.Text.RegularExpressions;
-----------------------------------------------------添加应用---------------------
StringDictionary SD = null;
IList<StringDictionary> sdList = new List<StringDictionary>();
System.Text.RegularExpressions.Regex reg = new System.Text.RegularExpressions.Regex(strReg, RegexOptions.IgnoreCase);
if (reg.IsMatch(strHtml))
{
System.Text.RegularExpressions.MatchCollection groupList = reg.Matches(strHtml);
for (int i = 0; i < groupList.Count; i++)
{
SD = new StringDictionary();
foreach (string item in regSD.Keys)
{//取得第几组 括号
SD.Add(item, groupList[i].Groups[KConvert.ToInt(regSD[item],1)].Value.Trim());
}
sdList.Add(SD);
}
}