日期:2014-05-18  浏览次数:20530 次

再求一个正则表达式
ArticleDetail.aspx?ArticleID=e69258bf-ef3c-43e0-8795-98f2a0b37652&Page=1

需要把它找出来替换成

xxxx/xxx/e69258bf-ef3c-43e0-8795-98f2a0b37652/index_2.shtml

这个正则如何写?最好能用regex.replace直接替换的


------解决方案--------------------
try

string yourStr = ...............;
string result = Regex.Replace(yourStr, @ "ArticleDetail\.aspx\?ArticleID=(? <arg> [^&]*).* ", "xxxx/xxx/${arg}/index_2.shtml ", RegexOptions.IgnoreCase);

------解决方案--------------------
不是吧,这个问题刚有人问过
------解决方案--------------------
Regex.Replace( @ "ArticleDetail\.aspx\?ArticleID=(? <arg> [^&]*).* ", "xxxx/xxx/${arg}/index_2.shtml);
------解决方案--------------------
string str = "ArticleDetail.aspx?ArticleID=e69258bf-ef3c-43e0-8795-98f2a0b37652 ";
str = System.Text.RegularExpressions.Regex.Replace (str, "ArticleDetail.aspx\\?ArticleID=([^$]*?)$ ", "xxx/xxx/$1 ");
Response.Write(str);
楼上的太快了
------解决方案--------------------
看看
------解决方案--------------------
try

string yourStr = ..............;
string result = Regex.Replace(yourStr, @ "ArticleDetail\.aspx\?ArticleID=(? <arg> [^&]*)&Page=(? <num> \d+) ",new MatchEvaluator(regReplace) , RegexOptions.IgnoreCase);


private string regReplace(Match m)
{
return "xxxx/xxx/ " + m.Groups[ "arg "].Value + "/index_ " + (Convert.ToInt32(m.Groups[ "num "].Value) + 1) + ".shtml ";
}
------解决方案--------------------
这里涉及到类型转换和计算,所以无法直接用Regex.Replace做到,可以用委托实现,如上,也可以把需要的两部分提取出来,处理后拼接字符串来实现
------解决方案--------------------
非得用 Reg 迈?

试试这个,更直观,专门处理 URL

string oldUrl = "ArticleDetail.aspx?ArticleID=e69258bf-ef3c-43e0-8795-98f2a0b37652&Page=1 ";
System.Collections.Specialized.NameValueCollection queryString = HttpUtility.ParseQueryString(oldUrl.Substring(oldUrl.IndexOf( "? ")));
string newUrl = String.Format( "xxxx/xxx/{0}/index_{1}.shtml ", queryString[ "ArticleID "], queryString[ "Page "]);
------解决方案--------------------
那我后面给出的代码你试过了吗,得到的就是这个结果

string yourStr = ..............;
string result = Regex.Replace(yourStr, @ "ArticleDetail\.aspx\?ArticleID=(? <arg> [^&]*)&Page=(? <num> \d+) ",new MatchEvaluator(regReplace) , RegexOptions.IgnoreCase);


private string regReplace(Match m)
{
return "xxxx/xxx/ " + m.Groups[ "arg "].Value + "/index_ " + (Convert.ToInt32(m.Groups[ "num "].Value) + 1) + ".shtml ";
}
------解决方案--------------------
Response.Write( " </br> " + Regex.Replace( " <a href=\ "ArticleDetail.aspx?ArticleID=17708f69-ee59-4546-b891-c5ca81ffbc11&Page=1\ "> ", @ "^ <a\shref=[^\?]*\?[^=]+=(? <id> [^\& " "]*)\&Page=(? <page> [^ " "]*).*$ ", new MatchEvaluator(regstr),RegexOptions.IgnoreCase));

public string regstr(Match m)
{