正则表达式与Xml永远在一起 [英] Regex Vs Xml taking forever

查看:87
本文介绍了正则表达式与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永远在一起的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆