找回密码
 加入
搜索
查看: 970|回复: 3

关于正则表达式的两个问题

[复制链接]
发表于 2009-5-26 09:00:54 | 显示全部楼层 |阅读模式
本帖最后由 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

运行出错,请我这个脚本哪里有问题呢?
发表于 2009-5-26 09:27:13 | 显示全部楼层
$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[0] & @CRLF)
WEnd
FileClose($file2)
FileClose($file3)
ShellExecute("test-zx.txt")
按照楼主的意思改了一下
提示:楼主的程序需考虑兼容性,stringregexp这句,你确定所有的路径都是E开头,在此处可用一个标准文件路径的正则代替。
 楼主| 发表于 2009-5-26 12:44:35 | 显示全部楼层
非常感谢pcbar的回复,请问标准文件路径的正则表达式是怎样的?谢谢
 楼主| 发表于 2009-5-26 12:48:21 | 显示全部楼层
还请问:有没有什么设定使得StringRegExpReplace ( "test", "pattern", "replace", [ count ] )中,"replace"的内容也可以用正则表达式?
您需要登录后才可以回帖 登录 | 加入

本版积分规则

QQ|手机版|小黑屋|AUTOIT CN ( 鲁ICP备19019924号-1 )谷歌 百度

GMT+8, 2024-5-11 01:33 , Processed in 0.067958 second(s), 19 queries .

Powered by Discuz! X3.5 Licensed

© 2001-2024 Discuz! Team.

快速回复 返回顶部 返回列表