使用VBScript查找多个正则表达式模式

3
抱歉,我对正则表达式还不太熟悉,希望有人能帮忙。
以下是相关文件:
    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 Builder(位于http://gskinner.com/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
1个回答

0
一个快速修复你的正则表达式的方法是使用(?:作者|出版商)(.+)\.你需要在vbscript中用一个空字符串替换第一个匹配组。

感谢所有的帮助。我终于让我的脚本按照需要工作了。 - user1861982

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接