[已解决]正则提取txt文档内容~
本帖最后由 t895073 于 2012-6-5 12:38 编辑正则提取txt文档特定内容
txt内容为
------------------------------------------
<SCRIPT type=text/javascript>100</SCRIPT>
<TD>2011/01/10</TD>
<TD></TD>
<TD>2011/01/11</TD>
<TD>2011/01/12</TD>
<SCRIPT type=text/javascript>101</SCRIPT>
<TD>2011/01/20</TD>
<TD>2011/01/21</TD>
<TD></TD>
<TD>2011/01/23</TD>
<SCRIPT type=text/javascript>102</SCRIPT>
<TD>2011/02/10</TD>
<TD>2011/02/20</TD>
<TD>2011/02/21</TD>
<TD>2011/02/22</TD>
<SCRIPT type=text/javascript>103</SCRIPT>
<TD>2011/02/20</TD>
<TD>2011/02/19</TD>
<TD>2011/02/19</TD>
<TD>2011/01/20</TD>
------------------------------------------
我要抓出第三个<TD> </TD> 的内容
用msgbox 一次一个显示出来要怎么弄呢?
如: MSGBOX 第一次显示 2011/01/11 案确定后 在显示 "空"
在显示 2011/02/21 在显示2011/02/19 一直显示完!
谢谢 #include <array.au3>
$file=FileOpen(@ScriptDir&"\test.txt",0)
$str=FileRead($file)
FileClose($file)
$arr=StringRegExp($str,"(?ms)(\<SCRIPT[\s\S]+?(\<TD[\s\S]*?){2}\<TD\>)(.*?)(?=\<\/TD\>)",3)
;_ArrayDisplay($arr)
For $n=2 To UBound($arr) Step 3
MsgBox(0,"",$arr[$n])
Next试下 本帖最后由 haijie1223 于 2012-6-4 19:34 编辑
$string = _
'------------------------------------------' & @CRLF & _
'<SCRIPT type=text/javascript>100</SCRIPT>' & @CRLF & _
'<TD>2011/01/10</TD>' & @CRLF & _
'<TD></TD>' & @CRLF & _
'<TD>2011/01/11</TD>' & @CRLF & _
'<TD>2011/01/12</TD>' & @CRLF & _
'<SCRIPT type=text/javascript>101</SCRIPT>' & @CRLF & _
'<TD>2011/01/20</TD>' & @CRLF & _
'<TD>2011/01/21</TD>' & @CRLF & _
'<TD></TD>' & @CRLF & _
'<TD>2011/01/23</TD>' & @CRLF & _
'<SCRIPT type=text/javascript>102</SCRIPT>' & @CRLF & _
'<TD>2011/02/10</TD>' & @CRLF & _
'<TD>2011/02/20</TD>' & @CRLF & _
'<TD>2011/02/21</TD>' & @CRLF & _
'<TD>2011/02/22</TD>' & @CRLF & _
'<SCRIPT type=text/javascript>103</SCRIPT>' & @CRLF & _
'<TD>2011/02/20</TD>' & @CRLF & _
'<TD>2011/02/19</TD>' & @CRLF & _
'<TD>2011/02/19</TD>' & @CRLF & _
'<TD>2011/01/20</TD>' & @CRLF & _
'------------------------------------------'
$result = StringRegExp($string, "(?m)(?<=TD>).*(?=<)", 3)
For $i = 2 To UBound($result)-1 Step 4
MsgBox(0, "", $result[$i])
Next
Exit
感谢回覆!
请问不用Step的话.
有办法可以直接用正则就抓出第三个<TD></TD>内容吗? 3楼正解,学习了 $file=FileOpen(@ScriptDir&"\test.txt",0)
$str=FileRead($file)
FileClose($file)
$regexp=ObjCreate("vbscript.regexp")
With $regexp
.global=1
.multiline=1
.ignorecase=1
.pattern="(\<script[\s\S]+?(\<td[\s\S]*?){2}\<td\>)(.*?)(?=\<\/td\>)"
If .test($str) Then
$arr=.execute($str)
For $match In $arr
MsgBox(0,"",$match.submatches(2))
Next
EndIf
EndWith这个也可以试一下 T>\s+(?:<TD>.*\s+){2}<TD>(.*)</TD 回复 7# afan
A版很久不见啊!
afan 发表于 2012-6-4 20:19 http://www.autoitx.com/images/common/back.gif
看你的正则总能学到些东西 用 _ArrayDisplay() 不好吗?!... 本帖最后由 lixiaolong 于 2012-6-4 23:42 编辑
#include <Array.au3>
Local $Str = _
'<SCRIPT type=text/javascript>100</SCRIPT>' & @CRLF & _
'<TD>2011/01/10</TD>' & @CRLF & _
'<TD></TD>' & @CRLF & _
'<TD>2011/01/11</TD>' & @CRLF & _
'<TD>2011/01/12</TD>' & @CRLF & @CRLF & _
'<SCRIPT type=text/javascript>101</SCRIPT>' & @CRLF & _
'<TD>2011/01/20</TD>' & @CRLF & _
'<TD>2011/01/21</TD>' & @CRLF & _
'<TD></TD>' & @CRLF & _
'<TD>2011/01/23</TD>' & @CRLF & @CRLF & _
'<SCRIPT type=text/javascript>102</SCRIPT>' & @CRLF & _
'<TD>2011/02/10</TD>' & @CRLF & _
'<TD>2011/02/20</TD>' & @CRLF & _
'<TD>2011/02/21</TD>' & @CRLF & _
'<TD>2011/02/22</TD>' & @CRLF & @CRLF & _
'<SCRIPT type=text/javascript>103</SCRIPT>' & @CRLF & _
'<TD>2011/02/20</TD>' & @CRLF & _
'<TD>2011/02/19</TD>' & @CRLF & _
'<TD>2011/02/19</TD>' & @CRLF & _
'<TD>2011/01/20</TD>' & @CRLF
Local $Test = StringRegExp($str, '(?s)(?=<SCRIPT.*?/SCRIPT>\s+(?:<TD>(.*?)</TD>.*?){3})', 3)
_ArrayDisplay($Test, UBound($Test))
If Not @error Then
For $i = 0 To UBound($Test) - 1
MsgBox(0, '', $Test[$i])
Next
EndIf
(?s)T>(?:.*?>){5}([^<]+)
要做就做最简单的 回复 13# love5173
有些符号不知道怎么用,兄台能讲解一下你的正则思路么,谢谢! 回复 14# haijie1223
是根据T>后面的>符号次数来取值的,出现5个>后取后面不等于<的值 这时候就知道正则表达强
谢谢帮忙!!
页:
[1]