匹配之间匹配最短

时间:2016-09-06 22:10:01

标签: c# regex xmldocument greedy non-greedy

我使用C#来解析XML,这种情况发生在我身上。这不是我正在做的,但同样的想法。单线选项已开启。

所以,如果我有一个字符串:

Start xxx A xxx Pattern xxx End
Start xxx B xxx Pattern xxx End
Start xxx C xxx Pattern xxx End

如果我要提取"启动xxx B xxx模式xxx结束",我使用

(Start.*?B.*?Pattern.*?End)

但这件事真的很匹配:

Start xxx A xxx Pattern xxx End
Start xxx B xxx Pattern xxx End

所以我说,好吧,也许我应该使用:

.*(Start.*?B.*?Pattern.*?End)

这确实提取了我想要的东西,但如果我有

Start xxx A xxx Pattern xxx End
....
Start xxx B xxx Pattern xxx End
Start xxx C xxx Pattern xxx End

我将匹配以下所有内容,

Start xxx A xxx Pattern xxx End
....
Start xxx B xxx Pattern xxx End

然后提取我想要的东西。 我认为这个巨大的匹配使我的程序因某种原因而变慢。

所以有没有办法让我指明我想要所有的最短匹配,只需匹配

Start xxx B xxx Pattern xxx End

P.S。 " XXX"在字符串中没有任何模式。

我认为这是一个贪婪,非贪婪的问题,任何人都可以帮忙吗?

0 个答案:

没有答案