2012-11-29 39 views
3

对不起,但我对RegEx有点新意,希望有人能够提供帮助。使用vbscript查找多个正则表达式模式

文件中的问题:

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 

现在,我想找到任何与作者或出版商开始,只需删除字符,直到扩展。

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生成器在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 

回答

0

要快速解决您的正则表达式是使用(?:Author|Publisher)(.+)\.你将不得不在VBScript中一个空字符串替换第一个匹配的组。

+0

谢谢,那有效奇迹。 – user1861982

+0

我在脚本中试过这段代码,但没有发生任何事情。 – user1861982

+0

感谢您的帮助。我终于让我的脚本根据需要工作。 – user1861982