62,046
社区成员
发帖
与我相关
我的任务
分享
string str = "all(a=2,b=3)and dfdsafd all(c=4,d=5)";
string str = "ss and dfdsafd ff";
protected void Page_Load(object sender, EventArgs e)
{
string str = "all(a=2,b=3) and dfdsafd all(c=4,d=5)";
MatchEvaluator me = new MatchEvaluator(ABC);
Regex regex = new Regex(@"all\([a-zA-Z]=(?<a>\d+),[a-zA-Z]=(?<b>\d+)\)");
string r = regex.Replace(str, me);
Response.Write(r + "<br/>");
MatchCollection matches = regex.Matches(str);
foreach (Match match in matches)
Response.Write(match.Groups["a"].Value + " " + match.Groups["b"].Value + "<br/>");
}
int n = 1;
private string ABC(Match match)
{
if (n == 1)
{
n = -n;
return "ss";
}
else
{
n = -n;
return "ff";
}
}
string tempStr = @"all(a=2,b=3)and dfdsafd all(c=4,d=5)"; //File.ReadAllText(@"C:\Documents and Settings\Administrator\桌面\Test.txt", Encoding.GetEncoding("GB2312"));
string pattern = @"(?im)all\([^=]+=([^,]+),[^=]+=([^,]+)\)";
MatchCollection mc = Regex.Matches(tempStr, pattern, RegexOptions.IgnoreCase | RegexOptions.Multiline);
foreach (Match m in mc)
{
//循环输出
string title = m.Value;//all(a=2,b=3)
string num_1 = m.Groups[1].Value;//2
string num_2 = m.Groups[2].Value;//3
tempStr = Regex.Replace(tempStr,pattern,"ss");//循环替换
}