请教如何用正则提取用户名、密码、有效期[已解决]
本帖最后由 lsszmj 于 2012-6-7 01:56 编辑请教如何用正则提取用户名、密码、有效期 id:'fks_087066087083082065084080087064072084081071093084084070084084',
blogTitle:'id',
blogAbstract:'<P\>Username:EAV-63977143<BR\>Password:8j4aa2r3pk<BR\>Expiration:2012/10/10</P\><P\>Username:EAV-59363841<BR\>Password:jp2ne237bc<BR\>Expiration:2012/06/15</P\><P\>Username:EAV-49173126<BR\>Password:m46xhrxhfs<BR\>Expiration:2012/10/09</P\><P\>Username:EAV-59363841<BR\>Password:jp2ne237bc<BR\>Expiration:2012/06/15<BR\></P\>',
blogTag:'esetnod32,id',
blogUrl:'blog/static/204922502201253115027705',附上网址:http://lss53.blog.163.com/blog/static/204922502201253115027705/
不知何故,正则学习就是不得法门,老是做伸手党,谢谢各位帮个忙哈。 本帖最后由 haijie1223 于 2012-6-4 09:30 编辑
用户名:
(?m)(?<=Username:)\w+\-\d+
密码:
(?m)(?<=Password:)\w+
有效期:
(?m)(?<=Expiration:)\d{4}\/\d{2}\/\d{2}
#include <INet.au3>
#include <array.au3>
Local $S, $usename, $password, $Expiration, $a
$S = _INetGetSource("http://lss53.blog.163.com/blog/static/204922502201253115027705/")
$usename = StringRegExp($S, "(?m)(?<=Username:)\w+\-\d+", 3)
$password = StringRegExp($S, "(?m)(?<=Password:)\w+", 3)
$Expiration = StringRegExp($S, "(?m)(?<=Expiration:)\d{4}\/\d{2}\/\d{2}", 3)
If UBound($usename) > 0 Then
Dim $a
For $i = 0 To UBound($usename) - 1
$a[$i] = $usename[$i]
$a[$i] = $password[$i]
$a[$i] = $Expiration[$i]
Next
EndIf
_ArrayDisplay($a)
回复 2# haijie1223
一起整呢?搞到一个数组中,谢谢! #include <Inet.au3>
#include <Array.au3>
Local $url="http://lss53.blog.163.com/blog/static/204922502201253115027705/"
Local $html=_INetGetSource($url)
Local $array=StringRegExp($html,"<P>(User[^<]+)<BR>(Pass[^<]+)<BR>(Expiration[^<]+)</P>",3)
_ArrayDisplay($array) 回复 3# lsszmj
#include <INet.au3>
#include <array.au3>
Local $S, $usename, $password, $Expiration, $a
$S = _INetGetSource("http://lss53.blog.163.com/blog/static/204922502201253115027705/")
$usename = StringRegExp($S, "(?m)(?<=Username:)\w+\-\d+", 3)
$password = StringRegExp($S, "(?m)(?<=Password:)\w+", 3)
$Expiration = StringRegExp($S, "(?m)(?<=Expiration:)\d{4}\/\d{2}\/\d{2}", 3)
If UBound($usename) > 0 Then
Dim $a
For $i = 0 To UBound($usename) - 1
$a[$i] = $usename[$i]
$a[$i] = $password[$i]
$a[$i] = $Expiration[$i]
Next
EndIf
_ArrayDisplay($a)
#include <Array.au3>
Local $Str = _
"id:'fks_087066087083082065084080087064072084081071093084084070084084'," & @CRLF & _
" blogTitle:'id'," & @CRLF & _
" blogAbstract:'<P\>Username:EAV-63977143<BR\>Password:8j4aa2r3pk<BR\>Expiration:2012/10/10</P\><P\>Username:EAV-59363841<BR\>Password:jp2ne237bc<BR\>Expiration:2012/06/15</P\><P\>Username:EAV-49173126<BR\>Password:m46xhrxhfs<BR\>Expiration:2012/10/09</P\><P\>Username:EAV-59363841<BR\>Password:jp2ne237bc<BR\>Expiration:2012/06/15<BR\></P\>'," & @CRLF & _
" blogTag:'esetnod32,id'," & @CRLF & _
" blogUrl:'blog/static/204922502201253115027705',"
Local $Test = StringRegExp($str, '(?:Username|Password|Expiration):([^<]+)', 3)
If Not @Error Then _ArrayDisplay($Test, UBound($Test))
本帖最后由 lsszmj 于 2012-6-4 09:57 编辑
回复 4# shqf
回复 5# haijie1223
回复 6# lixiaolong
4#的只提取了三组并包含了前面的关键字,谢谢;
5#的不是我想要的效果,让我也有不少收获,谢谢;
6#的有重复问题,匹配时重复了1倍,谢谢。 回复 7# lsszmj
你要这么说那你真的就是标准的伸手党,别人都给你代码了,剩下的只是你自己稍微加工一下。
你要吃饭,别人给你馒头了,难道还要喂你吃? 代码整成这样时,6#会重复,#Include <Array.au3>
$oMyError = ObjEvent("AutoIt.Error","MyErrFunc")
$oHTTP = ObjCreate("winhttp.winhttprequest.5.1")
$Url="http://lss53.blog.163.com/blog/static/204922502201253115027705/"
$a=$oHTTP.Open("GET",$Url)
$oHTTP.Send()
$HTMLSource = $oHTTP.Responsetext
MsgBox(0,'',$HTMLSource)
Func MyErrFunc()
$HexNumber=hex($oMyError.number,8)
Msgbox(0,"","We intercepted a COM Error !" & @CRLF & _
"Number is: " & $HexNumber & @CRLF & _
"Windescription is: " & $oMyError.windescription )
$_eventerror = 1
Endfunc
Local $Test = StringRegExp($HTMLSource, '(?:Username|Password|Expiration):([^<]+)', 3)
If Not @Error Then _ArrayDisplay($Test, UBound($Test)) 回复 8# haijie1223
让兄弟见笑了哈,我在这一块还真是个白痴哦 正在学习正则,学习楼上几位大大的代码 回复 10# lsszmj
别叫我兄弟,老哥你今年贵庚啊? 回复 9# lsszmj
不是代码重复,是网页源码中在还有四组相同的用户名、口令等内容啊。你要提取网页中显示 的4组,稍改一下就行了啊。我的代码正则改成如下,只取显示的四级:Local $array=StringRegExp($html,"<P>Username:([\w-]+)<BR>Password:(\w+)<BR>Expiration:([\d/]+)<",3) 再次感谢各位,我没看仔细哈 本帖最后由 lixiaolong 于 2012-6-4 13:52 编辑
回复 9# lsszmj
试试这个.#Include <Array.au3>
$oMyError = ObjEvent("AutoIt.Error","MyErrFunc")
$oHTTP = ObjCreate("winhttp.winhttprequest.5.1")
$Url="http://lss53.blog.163.com/blog/static/204922502201253115027705/"
$a=$oHTTP.Open("GET",$Url)
$oHTTP.Send()
$HTMLSource = $oHTTP.Responsetext
MsgBox(0,'',$HTMLSource)
Func MyErrFunc()
$HexNumber=hex($oMyError.number,8)
Msgbox(0,"","We intercepted a COM Error !" & @CRLF & _
"Number is: " & $HexNumber & @CRLF & _
"Windescription is: " & $oMyError.windescription )
$_eventerror = 1
Endfunc
Local $Test = StringRegExp($HTMLSource, '(?s)(?:Username|Password|Expiration):([^<]+)<(?!.*?\1)', 3)
If Not @Error Then _ArrayDisplay($Test, UBound($Test))