2012-11-29 2 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를이 코드 조각을 사용하지만이 작업을 수행 할 수있는보다 효율적인 방법이 있는지 궁금 무엇입니까?

Apples.A.Tasty.Treat.Author-JoeDirt.doc 
    Cooking.with.Apples.Publisher-Oscar.Publishing.txt 
    Candied.Treats.Author-JenBloc.Publisher-Event.docx 

지금, 나는 저자 또는 출판사로 시작하는 것을 발견하고 간단하게 확장 할 때까지 텍스트를 삭제할 : 스크립트가 위 완료되면

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 

, 나는 다음과 같은 파일 목록을 가지고있다.

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의 내선 지금 손실에 내 패턴을 테스트 http://gskinner.com/RegExr에서 RegExr Builder를 사용하고있다

myRegEx.Pattern = (?:Author|Publisher)[\w-](\S*\B[^][\w-].) 

    Apples.A.Tasty.Treat.doc 
    Cooking.with.Apples.txt 
    Candied.Treats. 

파일 만입니다 스트립 . 마지막으로 내 패턴이 예상대로 작동하면 어떻게하면 내 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

정규식에 대한 빠른 수정 당신은 VBScript를에서 빈 문자열과 일치하는 첫 번째 그룹을 교체해야합니다 (?:Author|Publisher)(.+)\.을 사용하는 것입니다.

+0

감사합니다. 그 놀라운 일들. – user1861982

+0

스크립트에서이 코드를 시도했지만 아무 일도 없었습니다. – user1861982

+0

모든 도움에 감사드립니다. 드디어 필요에 따라 스크립트가 생겼습니다. – user1861982

관련 문제