aa.txt
****************
<tr>
<td>2</td>
<tr>
<td>2</td>
<tr>
<td>2</td>
****************
をVBSで置換し
****************
<tr><td>2</td>
<tr><td>2</td>
<tr><td>2</td>
****************
のように置換したいのですが
****************
Dim objFile ' 対象ファイル
Dim oldText ' 置換前テキスト
Dim newText ' 置換後テキスト
Dim objFSO ' ファイルシステムオブジェクト
Dim objRep ' 正規表現オブジェクト
Dim repText ' 置換後文字列
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.OpenTextFile("aa.txt")
oldText = objFile.ReadAll
repText = "<tr><td>"
Set objRep = New RegExp
objRep.Multiline = True
objRep.Pattern = ".*<tr>.*\r\n.*<td>"
newText = objRep.replace(oldText, repText)
objFile.Close
Set objFile = objFSO.CreateTextFile("abc.html")
objFile.WriteLine (newText)
objFile.Close
****************
のVBSを走らせると
****************
<tr><td>2</td>
<tr>
<td>2</td>
<tr>
<td>2</td>
****************
このような結果となってしまいます
正規表現の部分をどうすれば理想のように置換できるでしょうか?
Global プロパティを True に設定すると、期待した動作をします。
Dim repText ' 置換後文字列 Set objFSO = CreateObject("Scripting.FileSystemObject") Set objFile = objFSO.OpenTextFile("aa.txt") oldText = objFile.ReadAll repText = "<tr><td>" Set objRep = New RegExp objRep.Multiline = True objRep.Pattern = ".*<tr>.*\r\n.*<td>" objRep.Global = True ' ★これを追加する newText = objRep.replace(oldText, repText) objFile.Close Set objFile = objFSO.CreateTextFile("abc.html") objFile.WriteLine (newText) objFile.Close
以下、リファレンスの URL です。
http://msdn.microsoft.com/ja-jp/library/cc392451.aspx
ありがとうございます!
2013/03/03 14:29:46