使用C#Regex删除div标签中包含的文本(Remove text enclosed in an div tag using C# Regex)

编程入门 行业动态 更新时间:2024-10-18 14:21:28
使用C#Regex删除div标签中包含的文本(Remove text enclosed in an div tag using C# Regex)

我有一个字符串如下: string chart = "<div id=\"divOne\">Label.</div>;" 这是在没有我控制的情况下动态生成的,并且想要删除文本“Label”。 来自封闭的div元素。

我尝试了以下但我的正则表达式知识仍然限于让它工作: System.Text.RegularExpressions.Regex.Replace(chart, @"/(<div[^>]+>)[^<]+(<\/div>)/i", "");

I have a string as follows: string chart = "<div id=\"divOne\">Label.</div>;" which is generated dynamically without my control and would like to remove the text "Label." from the enclosing div element.

I tried the following but my regex knowledge still limited to get it working: System.Text.RegularExpressions.Regex.Replace(chart, @"/(<div[^>]+>)[^<]+(<\/div>)/i", "");

最满意答案

你的正则表达式对我来说很好,(但是不要指定'/.../i'分隔符和修饰符)。 并使用'$1$2'作为替换字符串:

var re = new System.Text.RegularExpressions.Regex(@"(?i)(<div[^>]+>)[^<]+(<\/div>)"); var text = regex.Replace(text, "$1$2");

Your regex looks good to me, (but don't specify the '/.../i' delimiters and modifier). And use '$1$2' as your replacement string:

var re = new System.Text.RegularExpressions.Regex(@"(?i)(<div[^>]+>)[^<]+(<\/div>)"); var text = regex.Replace(text, "$1$2");

更多推荐

本文发布于:2023-08-04 06:44:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1412532.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:文本   标签   div   Regex   tag

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!