3

申し訳ありませんが、私は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-]+\.

これは、出版社名、出版年、書籍番号の 2 番目の部分を追加する追加のピリオドがある場合を除いて、ほとんどのファイルで機能します。

    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.

http://gskinner.com/RegExrの RegExr Builder を使用してパターンをテストしていますが、今は途方に暮れています。最後に、パターンが期待どおりに機能したら、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
4

1 に答える 1

0

正規表現の簡単な修正は(?:Author|Publisher)(.+)\.、vbscript で最初に一致するグループを空の文字列に置き換える必要があります。

于 2012-11-29T04:46:25.023 に答える