Regex 如何替换字符串的模式?
我有以下输入字符串:Regex 如何替换字符串的模式?,regex,vbscript,Regex,Vbscript,我有以下输入字符串: Hello <foo> context61 Hi: context:file/ hello context715: context666:file/ foo 我尝试了无数次,但说到正则表达式,我只是个傻瓜。有人能帮忙吗?试试下面的vbscript代码: str="Hello <foo> context61 Hi: context:file/ hello context715: context666:file/ foo" Set oreg = N
Hello <foo> context61 Hi: context:file/ hello context715: context666:file/ foo
我尝试了无数次,但说到正则表达式,我只是个傻瓜。有人能帮忙吗?试试下面的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
str=“Hello context61 Hi:context:file/Hello context715:context666:file/foo”
Set oreg=New RegExp
oreg.IgnoreCase=False
oreg.Global=True
oreg.Pattern=“context\d*\:”上下文后跟0或更多数字,后跟冒号
'oreg.Pattern=“context[a-zA-Z0-9]*?\:”,或者我们可以将此作为模式,它也将覆盖字母表
result=oreg.Replace(str,“”)将字符串“str”中的每个匹配模式替换为空字符串
MsgBox结果
输出:
尝试以下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
str=“Hello context61 Hi:context:file/Hello context715:context666:file/foo”
Set oreg=New RegExp
oreg.IgnoreCase=False
oreg.Global=True
oreg.Pattern=“context\d*\:”上下文后跟0或更多数字,后跟冒号
'oreg.Pattern=“context[a-zA-Z0-9]*?\:”,或者我们可以将此作为模式,它也将覆盖字母表
result=oreg.Replace(str,“”)将字符串“str”中的每个匹配模式替换为空字符串
MsgBox结果
输出:
“Set matches=oreg.Execute(str)”不是必需的。@registesrosis您是正确的。我是先测试比赛的。忘了把它取下来。我将更新代码。“oreg.IgnoreCase=False”也可能被删除,因为默认情况下.IgnoreCase为False。@Kira,顺便问一下,第一次测试是否更有效?e、 g.:
If-oreg.Test(str)然后oreg.Replace…
@zig-Yes,通过使用If语句,您可以首先测试是否存在匹配项。如果返回true,则替换,否则不替换。但是,即使没有匹配项,当前代码也不会抛出错误。“Set matches=oreg.Execute(str)”不是必需的。@regisdesrosis您是正确的。我是先测试比赛的。忘了把它取下来。我将更新代码。“oreg.IgnoreCase=False”也可能被删除,因为默认情况下.IgnoreCase为False。@Kira,顺便问一下,第一次测试是否更有效?e、 g.:If-oreg.Test(str)然后oreg.Replace…
@zig-Yes,通过使用If语句,您可以首先测试是否存在匹配项。如果返回true,则替换,否则不替换。但是,即使没有匹配项,当前代码也不会抛出错误。