匹配和替换两个单词之间的单词

时间:2013-01-15 16:48:06

标签: regex asp-classic vbscript

我有这个字符串,例如:

one two three four START four five four five six END seven

我想在其中搜索单词“four”以用REPLACED替换它,这将给出

one two three four START REPLACED five REPLACED five six END seven

我知道START(.*)END会在分隔符和分隔符之间给出单词。 我试过了START(?<four>)END,但它什么都没有。

我在Vbscript工作。

2 个答案:

答案 0 :(得分:5)

(?<name>适用于命名组。

您需要的是前瞻和后置断言,以匹配START.*.*END前后条件,而不实际匹配它们。

Dim input = "one two three four START four five four five six END seven"
Dim output = Regex.Replace(input, "(?<=START.*)four(?=.*END)", "test")

收益率:one two three four START test five test five six END seven

答案 1 :(得分:0)

为什么要使用正则表达式?

Dim oldStr = "one two three four START four five four five six END seven"

Dim newStr = oldStr.Replace("four", "REPLACED")