我有一个字符串如下: 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");更多推荐
发布评论