0

3文字の単語をすべて新しい文字列に置き換えたい。MsgBox表示変数threeLetterWordに新しい値がありますが、テキストは変更されません。

Sub ShowThreeLetterWords()

Set threeLetterRegExp = New RegExp
   threeLetterRegExp.Pattern = "\b[a-zA-Z]{3}\b"
   threeLetterRegExp.Global = True

   Dim threeLetterWords As MatchCollection
   Set threeLetterWords = threeLetterRegExp.Execute(ActiveDocument.Range)

   For Each threeLetterWord In threeLetterWords
      threeLetterWord = threeLetterRegExp.Replace(threeLetterWord, "sasa")
      MsgBox threeLetterWord
   Next threeLetterWord
 End Sub
4

1 に答える 1

1

正規表現は問題ありません。唯一の問題は、treeLetterWordは「foreach反復変数」であるため、ループ内で再度割り当てるべきではないということだと思います。

直接

MsgBox threeLetterRegExp.Replace(threeLetterWord, "sasa")

大丈夫なはず

于 2012-11-30T01:58:04.463 に答える