如何替换字符串模式?

zig*_*zig 1 regex vbscript

我有这个输入字符串:

Hello <foo> context61 Hi: context:file/  hello context715: context666:file/ foo
Run Code Online (Sandbox Code Playgroud)

我的目标是每次出现的替换(清理)替换context[anything]:为空字符串.这anything部分可能是空的.

输出应如下所示:

Hello <foo> context61 Hi: file/  hello  file/ foo
Run Code Online (Sandbox Code Playgroud)

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

Pot*_*toツ 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
Run Code Online (Sandbox Code Playgroud)

输出:

在此输入图像描述