本帖最后由 jchang 于 2009-5-28 09:40 编辑
假设文本1.txt的内容有多行,形式如下:
1."," ","E:\TA-UMA-GGR.jsm","E:\ABC.jsm",May-22-2009 17:32:51,May-22-2009
.......
20.Z - 11800'"," "," ","E:\TA-UMA-GGR.jsm","E:\DFE.jsm",May-22-2009 17:32:51,
现需要提取其中以"E:\TA-UMA-GGR.jsm","E:\ABC.jsm"形式的内容写入另一个文本2.txt,我尝试了两种方法:
方法一、使用$T = StringRegExpReplace,脚本如下:
$file2 = FileOpen("1.TXT", 0)
If $file2 = -1 Then
MsgBox(0, "错误", "不能打开文件.")
Exit
EndIf
FileDelete("test-zx.txt")
$file3 = FileOpen("test-zx.txt", 1)
If $file3 = -1 Then
MsgBox(0, "错误", "不能打开文件.")
Exit
EndIf
While 1
$line2 = FileReadline($file2)
If @error = -1 Then ExitLoop
$T = StringRegExpReplace($line2, '.*\"E:\\.*\"E.*', '\"E:\\.*\"E.*(jsm)', 0)
FileWrite($file3,$T& @CRLF)
If @error Then
MsgBox(0,'',"X")
EndIf
Wend
问题:使用StringRegExpReplace函数后把每行都替换成了字符“\"E:\\.*\"E.*(jsm)'”,而不是:E:\TA-UMA-GGR.jsm","E:\ABC.jsm,请我有没有什么设定使得StringRegExpReplace ( "test", "pattern", "replace", [ count ] )中,"replace"的内容也可以用正则表达式?
方法二:使用StringRegExp函数:
$file2 = FileOpen("1.TXT", 0)
If $file2 = -1 Then
MsgBox(0, "错误", "不能打开文件.")
Exit
EndIf
FileDelete("test-zx.txt")
$file3 = FileOpen("test-zx.txt", 1)
If $file3 = -1 Then
MsgBox(0, "错误", "不能打开文件.")
Exit
EndIf
While 1
$line2 = FileReadline($file2)
If @error = -1 Then ExitLoop
$T = StringRegExp($line2, '\"E:\\.*\"E.*(jsm)', 2)
MsgBox(0,'',$T[0])
FileWrite($file3,$T[0]& @CRLF)
If @error Then
MsgBox(0,'',"X")
EndIf
Wend
运行出错,请我这个脚本哪里有问题呢? |