2010-11-18 43 views
0

某处文本排除我有一个带HREFC#正则表达式负先行从比赛

<a href="image-CoRRECTME.aspx?ALSO=ME&leaveme=<%= MyClass.Text %>">somelink</a> 

我需要替换HREF attribue文本除的<%%含量为小写链接> brakets

应该是这样的:

<a href="image-correctme.aspx?also=me&leaveme=<%= MyClass.Text %>">somelink</a> 

我想是这样的.. "href=\"([^\"]*[A-Z]+[^\"]*)(?:(?<!.*<%[^%]*%>.*)))\""但它不工作。

谢谢!

+2

哇,哇,慢下来。你真的在解析aspx文件吗?或者你实际上正在执行该页面,并试图通过后面的代码替换href中的任何值?你要把那个正则表达式替换放在哪里? – BeemerGuy 2010-11-18 23:32:32

+0

我只需要在链接小写所有 – Sergey 2010-11-18 23:40:02

+0

可能重复[C#正则表达式查找和替换链接只有大写字符,不匹配排除](http://stackoverflow.com/questions/4219017/c-正则表达式查找和替换链接只带大写字符和) – 2010-11-19 03:13:39

回答

3

试试这个,如果你还没有计算出来了。

private void test() 
{ 
    string t = @"<a href=""image-CoRRECTME.aspx?ALSO=ME&leaveme=<%= MyClass.Text %>&test2=<%= MyClass2.Text %>&last_test=nothing"">somelink</a>"; 
    string fixed_string = Regex.Replace(t, "(?<=href=\"|href=\"[^\"]*%>)([^\"]*?)(?=<%|\")", TestMatchEvaluator); 
} 

private string TestMatchEvaluator(Match m) 
{ 
    return m.Value.ToLower(); 
} 
+0

+1不错的工作。我在[我的回复]中采用了不同的方法(http://stackoverflow.com/questions/4219017/c-regular-expressions-find-and-replace-links-with-only-uppercase-characters-and/4221722#4221722 )给OP的另一个问题。 – 2010-11-19 03:34:04

+0

哇,真棒,非常感谢你! – Sergey 2010-11-20 00:09:37

0

像这样的东西应该为你工作...

Regex test = new Regex(@"(?<=(href\=""))[^<]+", RegexOptions.Compiled); 

string htmlCode = @"<a href=""image-CoRRECTME.aspx?ALSO=ME&leaveme=<%= MyClass.Text %>"">somelink</a>"; 

string result = test.Replace(htmlCode, test.Match(htmlCode).Value.ToLower()); 
+0

参数异常解析“?<=(href \ =”))[^“] +” - Quantifier {x,y}什么也没有。 – Sergey 2010-11-18 23:39:07

+0

我错过了一个paren。现在修复。 – 2010-11-18 23:40:36

+0

瑞恩,这也使<% %>中的文字括号小写,我的问题是如何从比赛中排除它 – Sergey 2010-11-18 23:51:14