日期:2014-05-18 浏览次数:20541 次
Regex reg = new Regex(@"([^/]*)(.aspx)", RegexOptions.IgnoreCase); Match m = reg.Match(RawUrl); if (m.Success) { RawUrl = "/View.aspx?Code=" + m.Groups[1].Value; isMatch = true; }
string result = Regex.Replace(yourStr, @"/([^/.]+)\.aspx(\?)?", delegate(Match m){if(m.Groups[2].Value == "") return "/View.aspx?Code="+m.Groups[1].Value; return "/View.aspx?Code="+m.Groups[1].Value+"&";}, RegexOptions.IgnoreCase); richTextBox2.Text = result;
------解决方案--------------------
using System; using System.Text.RegularExpressions; class Program { static void Main() { foreach (string s in new string[]{"/A0021.aspx","/A0021.ASPX?Name=zhang&Year=2008"}) { string t = Convert(s); Console.WriteLine("{0} -> {1}", s, t); } } static string Convert(string s) { return Regex.Replace ( s, @"(?i)/([^/]*).aspx(\?)?", delegate (Match m) { if (m.Value.EndsWith("?")) return "/View.aspx?Code=" + m.Groups[1].Value + "&"; return "/View.aspx?Code=" + m.Groups[1].Value; } ); } } /* 程序输出: /A0021.aspx -> /View.aspx?Code=A0021 /A0021.ASPX?Name=zhang&Year=2008 -> /View.aspx?Code=A0021&Name=zhang&Year=2008 */