1

例如,我有这个字符串:

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

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

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

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

我在 Vbscript 工作。

4

2 回答 2

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

于 2013-01-15T16:55:29.353 回答
-1

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

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

Dim newStr = oldStr.Replace("four", "REPLACED")
于 2013-01-15T16:52:28.033 回答