请教大家一个超难的正则表达式.
我有如下文本:
<ul>
<li> <img class= 'parentNode '> <img>
<a href= '# ' name= '1174 ' onclick=javascript:skipie( '1174 ');>
1
</a>
</li>
<ul>
<li> <img class= 'parentNode '> <img>
<a href= '# ' name= '1175 ' onclick=javascript:skipie( '1175 ');> 1.1
</a>
</li>
<li> <img class= 'parentNode '> <img>
<a href= '# ' name= '1176 ' onclick=javascript:skipie( '1176 ');> 1.2
</a>
</li>
<ul>
<li> <img class= 'parentNode '> <img>
<a href= '# ' name= '1177 ' onclick=javascript:skipie( '1177 ');> 1.2.1
</a>
</li>
<li> <img class= 'parentNode '> <img>
<a href= '# ' name= '1178 ' onclick=javascript:skipie( '1178 ');> 1.2.2
</a>
</li>
</ul>
<li> <img class= 'parentNode '> <img>
<a href= '# ' name= '1179 ' onclick=javascript:skipie( '1179 ');> 1.3
</a>
</li>
</ul>
</ul>
要替换成:
<ul>
<li> <OBJECT type= "text/sitemap ">
<param name= "Name " value= "1 ">
<param name= "Local " value= "1174.html ">
</OBJECT>
</li>
<ul>
<li> <OBJECT type= "text/sitemap ">
<param name= "Name " value= "1.1 ">
<param name= "Local " value= "1175.html ">
</OBJECT>
</li>
<li> <OBJECT type= "text/sitemap ">
<param name= "Name " value= "1.2 ">
<param name= "Local " value= "1176.html ">
</OBJECT>
</li>
<ul>
<li> <OBJECT type= "text/sitemap ">
<param name= "Name " value= "1.2.1 ">
<param name= "Local " value= "1177.html ">
</OBJECT>
</li>
<li> <OBJECT type= "text/sitemap ">
<param name= "Name " value= "1.2.2 ">
<param name= "Local " value= "1178.html ">
</OBJECT>
</li>
</ul>
<li> <OBJECT type= "text/sitemap ">
<param name= "Name " value= "1.3 ">
<param name= "Local " value= "1179.html ">
</OBJECT>
</li>
</ul>
</ul>
如果简洁一点的话,就是:
<img class= 'parentNode '> <img>
<a href= '# ' name= '1174 ' onclick=javascript:skipie( '1174 ');> AAAAAA </a>
替换成:
<OBJECT type= "text/sitemap ">
<param name= "Name " value= "AAAAAA ">
<param name= "Local " value= "1174.html ">
</OBJECT>
但实际情况下,UL与LI嵌套比这复杂多了.
请教大家这个正则表达式怎么写.
[解决办法]
试下
string yourStr = ....................;
string ruleStr = @ " <li> ([\s\S]*?)(? <space> \s*)( <a\s+href= '# '\s+name= '(? <link> \d+) '\s+onclick=javascript:skipie\( '(\d+) '\);> )\s*(? <chapter> [^\s]+)\s* </a> (? <space2> \s*) </li> ";
string resultStr = Regex.Replace(yourStr, ruleStr,@ " <li> <OBJECT type= " "text/sitemap " "> ${space} <param name= " "Name " " value= " "${chapter} " "> ${space} <param name= " "Local " " value= " "${link}.html " "> ${space} </OBJECT> ${space2} </li> ",RegexOptions.IgnoreCase);
[解决办法]
string yourStr = ......;
string resultString = Regex.Replace(yourStr, " <img\\s+class=( ').+?\\1> \\s* <img> (\\s*) <a\\s+href= '# '\\s+name= '(.+?) '\\s+onclick=javascript:skipie\\( '(.+?) '\\).+?> [\\s|\\S]+?(\\d(\\.\\d)?)[\\s|\\S]+? </a> ", "$2 <OBJECT type=\ "text/sitemap\ "> $2 <param name=\ "Name\ " value=\ "$5\ "> $2 <param name=\ "Local\ " value=\ "$3.html\ "> ", RegexOptions.IgnoreCase);