Jump to content

Another RE problem


Recommended Posts

Hello, I'm back with a problem with regular expressions

I 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.htm

2790817 2732618_readme.htm

942146.reg

Windows6.html <== Error

Windows6.html <== Error

Can 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 by avechuche
Link to comment
Share on other sites

_IEAttach
$oImgCol=_IEImgGetCollection OR _IELinkGetCollection

For $oImg In $oImgCol
consolewrite($oImg.href & " " & $oImg.href @crlf)
next

no need to do regexp on html

Edited 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

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 by czardas
Link to comment
Share on other sites

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 here
RegExp tutorial: enough to get started
PCRE 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

Exactly. I've been there before and severly bitten at the same spot :

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 here
RegExp tutorial: enough to get started
PCRE 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

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 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

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)
Link to comment
Share on other sites

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

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 account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...