正则表达式与Xml永远在一起

编程入门 行业动态 更新时间:2024-10-25 23:28:12
本文介绍了正则表达式与Xml永远在一起的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

你好, 我正在运行一个正则表达式,该表达式在xml文档中搜索某个标签,然后替换其所有内容,即children/values标签. 我使用此正则表达式:

hello , i am running a regex that searches an xml document for a certain tag and then replaces all its contents i.e. children/values tags. i use this regex :

<(Car)>((\s)*(.)*)*</Car>

可能是什么问题? 我正在使用:

what could be the problem ? I am using :

Regex Car = new Regex (@"<(Car)>((\s)*(.)*)*</Car>"); Car.IsMatch (InputCarprofile)

后者要花很多时间.

The latter is taking lots of time.

推荐答案

此正则表达式有几个缺陷: 1)向我看,它有太多的组:(...) 2)太多*导致无法进行全面搜索(文档长度的n平方?) 3)如果是多行,则.将与行尾不匹配 4)必须有一些非贪婪的重复,否则,您将最终一次从头到尾匹配所有条目(如果您有多个Car元素,那可能不是您想要的. 对于简单的IsMatch()调用,以下足够了: This regular expression has several flaws: 1) looks to my that it has too many groups: (...) 2) too many * that lead to an exhautiive search (n-square of the document length?) 3) if it is multiline, the . will not match the end-of-line 4) there must be some non-greedy repetition, otherwise, you will finally match all from the first to the last entry at once (what is problably not what you want if you have more than one Car element. For the simple IsMatch() call, the following is sufficient: <Car>[\s\S]*?</Car>

我想念什么吗? 干杯 安迪

Do I miss something? Cheers Andi

更多推荐

正则表达式与Xml永远在一起

本文发布于:2023-10-17 13:18:21,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1501034.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:正则表达式   Xml

发布评论

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

>www.elefans.com

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