Regex 使用vbscript查找多个正则表达式模式
对不起,我对RegEx有点陌生,希望有人能帮我 有疑问的文件:Regex 使用vbscript查找多个正则表达式模式,regex,vbscript,Regex,Vbscript,对不起,我对RegEx有点陌生,希望有人能帮我 有疑问的文件: Apples.A.Tasty.Treat.Author-JoeDirt.doc Cooking with Apples Publisher-Oscar Publishing.txt Candied.Treats.Author-JenBloc.Publisher-Event.docx 我目前使用这段vbscript代码将文件名中的空格或破折号替换为句点,但我想知道是否有更有效的方法来实现这一点 Set
Apples.A.Tasty.Treat.Author-JoeDirt.doc
Cooking with Apples Publisher-Oscar Publishing.txt
Candied.Treats.Author-JenBloc.Publisher-Event.docx
我目前使用这段vbscript代码将文件名中的空格或破折号替换为句点,但我想知道是否有更有效的方法来实现这一点
Set colRegExMatches = strRegEx.Execute(objSourceFile.Name)
For Each objRegExMatch in colRegExMatches
strResult = InStr(objSourceFile.Name, objRegExMatch)
objTargetFile = Left(objSourceFile.Name, (strResult -1)) & objRegExMatch.Value
objTargetFile = Replace(objSourceFile.Name, " ", ".", 1, -1, 1)
objTargetFile = Replace(objSourceFile.Name, "-", ".", 1, -1, 1)
objSourceFile.Name = objTargetFile
Next
完成上述脚本后,我有以下文件列表:
Apples.A.Tasty.Treat.Author-JoeDirt.doc
Cooking.with.Apples.Publisher-Oscar.Publishing.txt
Candied.Treats.Author-JenBloc.Publisher-Event.docx
现在,我想找到任何以Author或Publisher开头的内容,只需删除文本直到扩展名
myRegEx.Pattern = (?:Author|Publisher)+[\w-]+\.
这主要适用于文件,除非有额外的时间添加出版商名称或出版年份或书号的第二部分
Apples.A.Tasty.Treat.doc
Cooking.with.Apples.Publishing.txt
Candied.Treats.docx
我尝试了这段代码,它似乎工作,但我必须指定文件扩展名
myRegEx.Pattern = (?:Author|Publisher)[\w-](\S*\B[^txt|docx|doc][\w-].)
如果我尝试以下操作,它会删除Candied.Treats文件的扩展名
myRegEx.Pattern = (?:Author|Publisher)[\w-](\S*\B[^][\w-].)
Apples.A.Tasty.Treat.doc
Cooking.with.Apples.txt
Candied.Treats.
我一直在使用上的RegExr生成器来测试我的模式,但现在我不知所措。最后,一旦我的模式按预期工作,如何在vbscript中使用它?我是否只需添加一个新行,如下所示
objTargetFile = Replace(objSourceFile.Name, "(?:Author|Publisher)[\w-](\S*\B[^txt|docx|pdf|doc][\w-].)", "", 1, -1, 1)
谢谢
这是新的vbscript代码,它似乎什么都不做
strFixChars = InputBox("Do you want to replace spaces, dashes and strip tags? (Y/N)", "Confirmation")
Set strRegEx = new RegExp
For Each objSourceFile in colSourceFiles
strFileExt = objFSO.GetExtensionName(objSourceFile)
objLogFile.WriteLine "Input File: " & objSourceFile.Name
strCount = Len(objSourceFile.Name)
strRegEx.Pattern = "(?:Author|Publisher)(.+)\."
strRegEx.IgnoreCase = True
strRegEx.Global = True
Set colRegExMatches = strRegEx.Execute(objSourceFile.Name)
For Each objRegExMatch in colRegExMatches
strResult = InStr(objSourceFile.Name, objRegExMatch)
objTargetFile = Left(objSourceFile.Name, (strResult -1)) & objRegExMatch.Value
If strFixChars = "Y" Then
objTargetFile = Replace(objSourceFile.Name, " ", ".")
objTargetFile = Replace(objSourceFile.Name, "-", ".")
objTargetFile = Replace(objSourceFile.Name, "(?:Author|Publisher)(.+)\.", "")
End If
objLogFile.WriteLine "Output File: " & objTargetFile
strFileList = strFileList & vbCrlf & objTargetFile
Next
Next
您的正则表达式的一个快速修复方法是使用
(?:Author | Publisher)(.+)\。
您必须用vbscript中的空字符串替换第一个匹配组。感谢所有帮助。我终于让我的脚本按需要工作了。