[Association(Name="pxb_zd_pxb_jf", Storage="_pxb_zd", ThisKey="lx_id", OtherKey="id", IsForeignKey=true)]
public
其中 public是在第2行,要求将public 替换成internal 如何写表达式,谢谢
我用正则表达式的经验啊
匹配的思路分为2种
一种是按格式匹配
例如 电话号码:010-11111111
那么他的匹配正则就应该是: [\d]{3}-[\d]{8}
第二种 按特殊的首位匹配
例如 有一段html代码我想获取2个span之间的内容 :
开始处<span>jasdfljasdfklajsdflkadsjf</span>结束处
那么匹配的方法就是:开始处<span>(.*?)</span>结束处
至于取值的用法
1.如何获取单个匹配项
string content = "123456789";
string pat = "\\d";
Regex regex = new Regex(pat);
Match match = regex.Match(content); // 获取匹配到的第一项
content = match.ToString();
2.如何获取多个匹配项
string content = "123456789";
string pat = "\\d";
Regex regex = new Regex(pat);
MatchCollection matchCollection = regex.Matches(content);
foreach (Match match in matchCollection)
{
match.ToString(); // 获取匹配集合的每项
}
3.如何获取匹配项中的一部分
string content = "123456789";
string pat = "1([\\d])3";
Regex regex = new Regex(pat);
MatchCollection matchCollection = regex.Matches(content);
foreach (Match match in matchCollection)
{
foreach (Group group in match.Groups)
{
group.Value; //通过group获取打括号的部分
}
}
protected void btnMatch3_Click(object sender, EventArgs e)
{
string strSrc="[Association(Name=\"pxb_zd_pxb_jf\", Storage=\"_pxb_zd\", ThisKey=\"lx_id\", OtherKey=\"id\", IsForeignKey=true)] "+System.Environment.NewLine+" public ";
ResultString = GetContentByHtml3(@"(\[Association.*\n*\r*.*\n*\r*.)public", strSrc,"public","internal");
}
private string GetContentByHtml3(string strRegEx, string strHtml,string strOrg,string strReplace)
{
if (string.IsNullOrEmpty(strRegEx)) { return strHtml; }
Regex reg = new Regex(strRegEx);
Match mat = reg.Match(strHtml);
if (mat.Success)
{
strHtml=mat.Value;
strHtml = strHtml.Replace(strOrg, strReplace);
//strHtml = mat.Value;
}
return strHtml;
}