avechuche Posted April 5, 2013 Share Posted April 5, 2013 (edited) Hello, I'm back with a problem with regular expressionsI have this html code (is more extensive, but it is always the same, "and I need to list the text found between "> and </a> with regular expressions.<img src="/icons/text.gif" alt="[TXT]" width="20" height="22"> <a href="2757817%202691586%20Installation%20instructions.htm"> 2,757,817 2,691,586 Installation instructions . htm </ a> <img width="20" height="22"> <a src="/icons/text.gif" alt="[TXT]" 2732618_readme.htm href="2790817%202732618_readme.htm"> 2790817 </a> <img width="20" height="22"> <a src="/icons/text.gif" alt="[TXT]" href="942146.reg"> 942146.reg </a> <img src="/icons/text.gif" alt="[TXT]" width="20" height="22"> <a href="Windows6.1-KB2757817-x64%20Installation%20instructions.htm"> Windows6.1-KB2757817-x64 Installation instructions.htm </a> <img src="/icons/text.gif" alt="[TXT]" width="20" height="22"> <a href="Windows6.1-KB976932-x64.html"> Windows6.1-KB976932-x64.html </a>$sFilter = "msu|html|reg|txt" $sString = '(?i)">([^< ].*?\.(?:' & $sFilter & '))</a>'but there is an error, this is the result.2757817 2691586 Installation instructions.htm2790817 2732618_readme.htm942146.regWindows6.html <== ErrorWindows6.html <== ErrorCan you help? THANKS!PD: You can also take data from <a href=" and ">, but not how to replace the "%20" for spaces with ER. Edited April 5, 2013 by avechuche Link to comment Share on other sites More sharing options...
jdelaney Posted April 5, 2013 Share Posted April 5, 2013 (edited) _IEAttach $oImgCol=_IEImgGetCollection OR _IELinkGetCollection For $oImg In $oImgCol consolewrite($oImg.href & " " & $oImg.href @crlf) next no need to do regexp on html Edited April 5, 2013 by jdelaney avechuche 1 IEbyXPATH-Grab IE DOM objects by XPATH IEscriptRecord-Makings of an IE script recorder ExcelFromXML-Create Excel docs without excel installed GetAllWindowControls-Output all control data on a given window. Link to comment Share on other sites More sharing options...
avechuche Posted April 5, 2013 Author Share Posted April 5, 2013 (edited) Yes, but i use InetRead, not _IExxxxx . It was my mistake not clarify Edited April 5, 2013 by avechuche Link to comment Share on other sites More sharing options...
jdelaney Posted April 5, 2013 Share Posted April 5, 2013 No, I went out on a limb to try to help in an easier way (my opinion) than regexp. IEbyXPATH-Grab IE DOM objects by XPATH IEscriptRecord-Makings of an IE script recorder ExcelFromXML-Create Excel docs without excel installed GetAllWindowControls-Output all control data on a given window. Link to comment Share on other sites More sharing options...
czardas Posted April 5, 2013 Share Posted April 5, 2013 (edited) This is not the solution the OP wants, but I should not have deleted it. Sorry to anyone who was reading it. Reposting. #include <array.au3> Local $sTest = '<img src="/icons/text.gif" alt="[TXT]" width="20" height="22"> <a href="2757817%202691586%20Installation%20instructions.htm"> 2,757,817 2,691,586 Installation instructions . htm </ a>' & _ '<img width="20" height="22"> <a src="/icons/text.gif" alt="[TXT]" 2732618_readme.htm href="2790817%202732618_readme.htm"> 2790817 </a>' & _ '<img width="20" height="22"> <a src="/icons/text.gif" alt="[TXT]" href="942146.reg"> 942146.reg </a>' & _ '<img src="/icons/text.gif" alt="[TXT]" width="20" height="22"> <a href="Windows6.1-KB2757817-x64%20Installation%20instructions.htm"> Windows6.1-KB2757817-x64 Installation instructions.htm </a>' & _ '<img src="/icons/text.gif" alt="[TXT]" width="20" height="22"> <a href="Windows6.1-KB976932-x64.html"> Windows6.1-KB976932-x64.html </a>' Local $aArray = StringRegExp($sTest, "(?i)([^>]+)(?:</\s?a)", 3) _ArrayDisplay($aArray) Edited April 5, 2013 by czardas operator64 ArrayWorkshop Link to comment Share on other sites More sharing options...
jchd Posted April 5, 2013 Share Posted April 5, 2013 Parsing html with REs is almost always deemed to failure some day. It's very fragile since web pages evolve constantly and it suffices of the slightliest change in cosmetics (addition of a bold attribute for instance, or insertion of meaningless whitespaces at unexpected places) to make your RE collapse. Use of _IE* functions makes your scripts much, much more stable when it come to dissect html and friends. This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe hereRegExp tutorial: enough to get startedPCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta. SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt) Link to comment Share on other sites More sharing options...
czardas Posted April 5, 2013 Share Posted April 5, 2013 I saw problems with my code. I imagine some really complicated expressions could be created which would capture most html patterns, but I do find this rather complicated, and get it wrong sometimes. operator64 ArrayWorkshop Link to comment Share on other sites More sharing options...
jchd Posted April 5, 2013 Share Posted April 5, 2013 Exactly. I've been there before and severly bitten at the same spot czardas 1 This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe hereRegExp tutorial: enough to get startedPCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta. SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt) Link to comment Share on other sites More sharing options...
jdelaney Posted April 5, 2013 Share Posted April 5, 2013 (edited) blam: #include <IE.au3> $sHTML = '<body>' & @CRLF & _ '<img src="/icons/text.gif" alt="[TXT]" width="20" height="22"><a href="275781720269158620Installation20instructions.htm"></a></img>' & @CRLF & _ '<img width="20" height="22"><a src="/icons/text.gif" alt="[TXT]" 2732618_readme.htm href="2790817202732618_readme.htm"></a></img>' & @CRLF & _ '<img width="20" height="22"><a src="/icons/text.gif" alt="[TXT]" href="942146.reg"></a></img>' & @CRLF & _ '<img src="/icons/text.gif" alt="[TXT]" width="20" height="22"><a href="Windows6.1-KB2757817-x6420Installation20instructions.htm"></a></img>' & @CRLF & _ '<img src="/icons/text.gif" alt="[TXT]" width="20" height="22"><a href="Windows6.1-KB976932-x64.html"> Windows6.1-KB976932-x64.html</a></img>' & @CRLF & _ "</body>" $oIE = _IECreate("about:blank",0,0) _IEBodyWriteHTML($oIE,$sHTML) $oImgCol = _IEImgGetCollection($oIE) $oLinkCol = _IELinkGetCollection($oIE) For $oImg In $oImgCol ConsoleWrite($oImg.src & @CRLF) Next For $oLink In $oLinkCol ConsoleWrite($oLink.href & " INNERHTML=" & $oLink.innerhtml & @CRLF) Next _iequit($oIE) or pop in the full html with _IEDocWriteHTML Edited April 5, 2013 by jdelaney IEbyXPATH-Grab IE DOM objects by XPATH IEscriptRecord-Makings of an IE script recorder ExcelFromXML-Create Excel docs without excel installed GetAllWindowControls-Output all control data on a given window. Link to comment Share on other sites More sharing options...
Malkey Posted April 5, 2013 Share Posted April 5, 2013 Maybe this. #include <array.au3> Local $sTest = '<img src="/icons/text.gif" alt="[TXT]" width="20" height="22"> <a href="2757817%202691586%20Installation%20instructions.htm"> 2,757,817 2,691,586 Installation instructions . htm </ a>' & _ '<img width="20" height="22"> <a src="/icons/text.gif" alt="[TXT]" 2732618_readme.htm href="2790817%202732618_readme.htm"> 2790817 </a>' & _ '<img width="20" height="22"> <a src="/icons/text.gif" alt="[TXT]" href="942146.reg"> 942146.reg </a>' & _ '<img src="/icons/text.gif" alt="[TXT]" width="20" height="22"> <a href="Windows6.1-KB2757817-x64%20Installation%20instructions.htm"> Windows6.1-KB2757817-x64 Installation instructions.htm </a>' & _ '<img src="/icons/text.gif" alt="[TXT]" width="20" height="22"> <a href="Windows6.1-KB976932-x64.html"> Windows6.1-KB976932-x64.html </a>' Local $aArray = StringRegExp(StringReplace($sTest, "%20", " "), 'href="([^"]+)"', 3) _ArrayDisplay($aArray) ileandros 1 Link to comment Share on other sites More sharing options...
kylomas Posted April 6, 2013 Share Posted April 6, 2013 Yes, but i use InetRead, not _IExxxxx . It was my mistake not clarify Then this may be of interest (similar to jdelaney's) #include <IE.au3> local $sHTML = fileread(@scriptdir & '\test1.txt') ConsoleWrite(_get_text($sHTML) & @LF) Func _get_text($html) Local $ohtml = ObjCreate('HTMLFILE'), $ret_str = '' If Not IsObj($ohtml) Then Return SetError(-1, 0, '') $ohtml.open() $ohtml.write($html) $ohtml.close() Local $oeles = _IETagNameAllGetCollection($ohtml) For $oele In $oeles if $oele.tagname = 'a' then $ret_str &= $oele.innerText & @lf Next return $ret_str EndFunc kylomas Forum Rules Procedure for posting code "I like pigs. Dogs look up to us. Cats look down on us. Pigs treat us as equals." - Sir Winston Churchill Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now