如何替换字符串模式?

时间:2017-06-28 11:54:14

标签: regex vbscript

我有这个输入字符串:

Hello <foo> context61 Hi: context:file/  hello context715: context666:file/ foo

我的目标是替换(清除)每次出现context[anything]:到空字符串。 anything部分可能是空的。

输出应如下所示:

Hello <foo> context61 Hi: file/  hello  file/ foo

我尝试过无尽的尝试,但在正则表达方面,我只是一个菜鸟。有人可以帮忙吗?

1 个答案:

答案 0 :(得分:7)

尝试使用以下vbscript代码:

str="Hello <foo> context61 Hi: context:file/  hello context715: context666:file/ foo"
Set oreg = New RegExp
oreg.IgnoreCase=False
oreg.Global=True
oreg.Pattern="context\d*\:"                'context followed by 0 or more digits followed by colon
'oreg.Pattern="context[a-zA-Z0-9]*?\:"     'or we can have this as pattern which will cover alphabets too
result = oreg.Replace(str,"")              'replaces every matched pattern in the string 'str' by an empty string
MsgBox result

输出:

enter image description here