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

在线等,正则匹配 Server.UrlEncode处理过的字符串
比如,有一个URL为http://localhost/blog/Tag.aspx?TagName=%ce%c4%d5%c2%b1%ea%c7%a9

怎么写正则匹配   "TagName= "   后面的那段字符串,如果排除 "/ "   又怎么写呢。。

------解决方案--------------------
抢 sf = 客客
------解决方案--------------------
try

TagName=(%\w{2})+

or

TagName=[^/]+
------解决方案--------------------
TagName=((?:%[a-zA-Z\d]{2})+)

排除哪里的 "/ " ??
------解决方案--------------------
using System.Text.RegularExpressions;

// Regex Match code for C#
void MatchRegex()
{
// Regex match
RegexOptions options = RegexOptions.None;
Regex regex = new Regex(@ "http(s)?://([\w-]+\.)*[\w-]*(/[\w- ./?%&=]*)? ", options);
string input = @ "http://csdn.net
http://www.aspxboy.com
http://www.DotNetJobs.CN
hhttp://www.DotNetJobs.CN/Tag/Default.aspx
hhttp://localhost/blog/Tag.aspx?TagName=%ce%c4%d5%c2%b1%ea%c7%a9
";

// Check for match
bool isMatch = regex.IsMatch(input);
if( isMatch )
{
// TODO: Do something with result
System.Windows.Forms.MessageBox.Show(input, "IsMatch ");
}

// Get match
Match match = regex.Match(input);
if( match != null )
{
// TODO: Do something with result
System.Windows.Forms.MessageBox.Show(match.Value, "Match ");
}

// Get matches
MatchCollection matches = regex.Matches(input);
for( int i = 0; i != matches.Count; ++i )
{
// TODO: Do something with result
System.Windows.Forms.MessageBox.Show(matches[i].Value, "Match ");
}

// Numbered groups
for( int i = 0; i != match.Groups.Count; ++i )
{
Group group = match.Groups[i];

// TODO: Do something with result
System.Windows.Forms.MessageBox.Show(group.Value, "Group: " + i);
}

// Named groups
string groupA = match.Groups[ "groupA "].Value;
string groupB = match.Groups[ "groupB "].Value;

// TODO: Do something with result
System.Windows.Forms.MessageBox.Show(groupA, "Group: groupA ");
System.Windows.Forms.MessageBox.Show(groupB, "Group: groupB ");
}
------解决方案--------------------
URL Rewrite我没有做过,只是回答过几个类似的帖子,看你的例子,正则那样写应该就没问题了,你这样试试看吧

<LookFor> ~/blog/TagName([^/]+)/ </LookFor>
<SendTo> ~/blog/TagView.aspx?TagName=$1 </SendTo>
------解决方案--------------------
study~~