【发布时间】:2015-12-02 06:34:25
【问题描述】:
我必须匹配包含一些短语的文本,并且它必须包含在两个常量之间。举个例子吧:
我的上限和下限是相同的,例如“开始”。现在有更多受“START”限制的文本块。我想捕获限制之间的文本,但捕获的文本必须包含一些短语。
START
text1 text1 text1 phrase1 text text
START
text2 text2 phrase2 text2
START
text3 phrase1
START
现在我想用短语 1 捕获文本。它是“text1 text1 text1 phrase1 text text”和“text3 phrase1”。
匹配的文本可能包含 START 词。
我的尝试(没有以正确的方式工作)是:START((.|\s)*?phrase1(.|\s)*?)START。
当START text1 text1 text1 phrase1 text text START 被捕获时,我将其保存到变量中,并使用“START”替换正则表达式方法,因此它看起来像这样:
START
text2 text2 phrase2 text2
START
text3 phrase1
START
在第二个循环之后我得到了:
START
text2 text2 phrase2 text2
START
当我得到第三个循环时(如果它有效,正则表达式应该返回 false)但我崩溃了。在一些在线正则表达式中,它显示不匹配,所以它与模式有问题。
编辑:添加的循环代码
set regEx = new RegEx
//sInputString is my text
regEx.pattern = "START((.|\s)*?phrase1(.|\s)*?)START"
set matches = regEx.Execute(sInputString)
do while (matches.count <> 0)
sInputString = regEx.Replace(sInputString, "START")
set matches = regEx.Execute(sInputString) //this line crashes after "ending loop"
//saving to array
loop
【问题讨论】:
-
任何特定的语言?
-
请发布您的代码。我觉得这与你在没有匹配时的处理方式有关
-
在VBA中使用。它使用 VBScript 正则表达式 5.5 库。我已经添加了代码。您认为(鄙视效率等)该模式应该有效吗?
标签: regex