gpt4 book ai didi

regex - 使用 vbscript 查找多个正则表达式模式

转载 作者:行者123 更新时间:2023-12-04 18:40:23 24 4
gpt4 key购买 nike

抱歉,我对 RegEx 有点陌生,希望有人能提供帮助。

有问题的文件:

    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

最佳答案

正则表达式的快速修复是使用 (?:Author|Publisher)(.+)\. 您必须在 vbscript 中用空字符串替换第一个匹配组。

关于regex - 使用 vbscript 查找多个正则表达式模式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13618883/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com