当前位置: 动力学知识库 > 问答 > 编程问答 >

c# - Replacing mulitple matches between 2 strings using regex

问题描述:

I want to do a Regex.Replace() to get rid of all the \r\n between the ul and /ul tags. Now I love writing RegEx as much as the next guy, But I can't for the life of me figure out the correct pattern to use. Any one have any idea what the pattern I need to match all instances of /r/n between the two tags using RegEx?

This is a note&nbsp;I am using to display bullets&nbsp;here are some examples\r\n<ul>\r\n<li>somethign</li>\r\n<li>somthing else</li>\r\n</ul>

网友答案:

There's probably a way to accomplish this is a pure regular expression, but one option is to use a regex and a custom match evaluator:

value = Regex.Replace(value, "<ul>.*?</ul>", (match) =>
    {
        return match.Value.Replace("\r\n", "");
    }, RegexOptions.Singleline);

And as a side point: Once you start thinking about parsing HTML fragments, you should consider using HtmlAgilityPack. It's not always necessary, but it can save trouble as you get more and more complex requirements.

分享给朋友:
您可能感兴趣的文章:
随机阅读: