如我现在有URL /A0021.aspx 要改写为 /View.aspx?Code=A0021
可以用以下代码
- C# code
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;}
而我现在有新的要求:如
要将
/A0021.aspx
改写为
/View.aspx?Code=A0021
还要可以将
/A0021.aspx?Name=zhang&Year=2008
改写为
/View.aspx?Code=A0021&Name=zhang&Year=2008
谢谢
------解决方案--------------------------------------------------------
d
------解决方案--------------------------------------------------------
上面写的应再多加一个条件
- C# code
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;
------解决方案--------------------------------------------------------
- C# code
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*/