我需要使用 RegEx 替换 C# 中的一些文本:
string strSText = "<P>Bulleted list</P><UL><P><LI>Bullet 1</LI><P></P><P>
<LI>Bullet 2</LI><P></P><P><LI>Bullet 3</LI><P></UL>"
基本上我需要摆脱
"<P>"
标签之间引入
"<UL><P><LI>",
"</LI><P></P><P><LI>" and
"</LI><P></UL>"
执行删除时,我还需要忽略这些标签之间的任何空格。
所以
"</LI><P></P><P><LI>", "</LI> <P></P><P><LI>", "</LI><P></P><P> <LI>" or
"</LI> <P> </P> <P> <LI>"
必须全部替换为
"</LI><LI>"
为此,我尝试使用以下 RegEx 匹配:
strSText = Regex.Replace(strSText, "<UL>.*<LI>", "<UL><LI>", RegexOptions.IgnoreCase);
strSText = Regex.Replace(strSText, "</LI>.*<LI>", "</LI><LI>",
RegexOptions.IgnoreCase);
strSText = Regex.Replace(strSText, "</LI>.*</UL>", "</LI></UL>",
RegexOptions.IgnoreCase);
但它执行“贪婪”匹配并导致:
"<P>Bulleted list</P><UL><LI>Bullet 3</LI></UL>"
然后我尝试使用“惰性”匹配:
strSText = Regex.Replace(strSText, "<UL>.*?<LI>", "<UL><LI>", RegexOptions.IgnoreCase);
strSText = Regex.Replace(strSText, "</LI>.*?<LI>", "</LI><LI>",
RegexOptions.IgnoreCase);
strSText = Regex.Replace(strSText, "</LI>.*?</UL>", "</LI></UL>",
RegexOptions.IgnoreCase);
这导致:
"<P>Bulleted list</P><UL><LI>Bullet 1</LI></UL>"
但我想要以下结果,它保留所有其他数据:
"<P>Bulleted list</P><UL><LI>Bullet 1</LI><LI>Bullet 2</LI><LI>Bullet 3</LI></UL>"