关于正则表达式的两个问题
本帖最后由 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 @errorThen
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)
FileWrite($file3,$T& @CRLF)
If @errorThen
MsgBox(0,'',"X")
EndIf
Wend
运行出错,请我这个脚本哪里有问题呢? $file2 = FileOpen("1.TXT", 0)
If $file2 = -1 Then
MsgBox(0, "错误", "不能打开文件.")
Exit
EndIf
$file3 = FileOpen("test-zx.txt", 2)
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)
if IsArray($T) Then FileWrite($file3, $T & @CRLF)
WEnd
FileClose($file2)
FileClose($file3)
ShellExecute("test-zx.txt")按照楼主的意思改了一下
提示:楼主的程序需考虑兼容性,stringregexp这句,你确定所有的路径都是E开头,在此处可用一个标准文件路径的正则代替。 非常感谢pcbar的回复,请问标准文件路径的正则表达式是怎样的?谢谢 还请问:有没有什么设定使得StringRegExpReplace ( "test", "pattern", "replace", [ count ] )中,"replace"的内容也可以用正则表达式?
页:
[1]