StrX() is a wrapper that extends SubStr()'s functionality. It accepts two strings for extremes ( begin & end ) and extracts the text in between them. It is much similar to RegExMatch( Str, "BeginStr(.*)EndStr", SubPat ), but the major difference is, StrX() allows flexibility on the final length of the resultant string. To be precise, it can trim/expand characters at either/both ends of the resultant string.
Quote:
Announcement: The current version 1.0 can auto-parse when used with While loop. Please checkout the updated examples.
StrX( H, BS,BO,BT, ES,EO,ET, NextOffset ) Parameters 1 ) H = HayStack. The "Source Text" 2 ) BS = BeginStr. Pass a String that will result at the left extreme of Resultant String 3 ) BO = BeginOffset. Number of Characters to omit from the left extreme of "Source Text" while searching for BeginStr Pass a 0 to search in reverse ( from right-to-left ) in "Source Text" If you intend to call StrX() from a Loop, pass the same variable used as 8th Parameter, which will simplify the parsing process. 4 ) BT = BeginTrim. Number of characters to trim on the left extreme of Resultant String Pass the String length of BeginStr if you want to omit it from Resultant String Pass a Negative value if you want to expand the left extreme of Resultant String 5 ) ES = EndStr. Pass a String that will result at the right extreme of Resultant String 6 ) EO = EndOffset. Can be only True or False. If False, EndStr will be searched from the end of Source Text. If True, search will be conducted from the search result offset of BeginStr or from offset 1 whichever is applicable. 7 ) ET = EndTrim. Number of characters to trim on the right extreme of Resultant String Pass the String length of EndStr if you want to omit it from Resultant String Pass a Negative value if you want to expand the right extreme of Resultant String 8 ) NextOffset : A name of ByRef Variable that will be updated by StrX() with the current offset, You may pass the same variable as Parameter 3, to simplify data parsing in a loop Here follows real world examples that demonstrates StrX()'s functionality: Example 1 : A Script to retrieve real-time details of last 15 posts made in our forum.
Code:
UrlDownloadToFile, http://www.autohotkey.com/forum/rss.php, ahkrss.xml ; 01 FileRead, xml, ahkrss.xml ; 02 While Item := StrX( xml , "<item>" ,N,0, "</item>" ,1,0, N ) ; 03 Title := StrX( Item, "<title>",1,7, "</title>",1,8 ) ; 04 , Link := StrX( Item, "<link>" ,1,6, "</link>" ,1,7 ) ; 05 , List .= "`n`n" A_Index ")`t" Title "`n`t" Link ; 06 MsgBox, 64, Latest Posts on AHK Forum, %List% ; 07
Quote:
Note: The result of above script may contain HTML formatting like below: 15) Ask for Help :: &amp;quot;Jump to&amp;quot; video frame (i.e. &amp;quot;seek&amp;quot; You may use UnHTM() on Title to convert it to proper text.
Example 2 : Download and extract links from a Google Search Result
Code:
UrlDownloadToFile, % "http://www.google.com/search?hl=en&lr=&safe=active&rlz=1C1GGLS_enIN" . "307IN307&num=10&q=site:autohotkey.com&aq=f&oq=&aqi=", Google.htm FileRead, html, Google.htm While Item := StrX( html, "<h3 class=r><a href=",N,0, "<li class=g>",1,12, N ) Sub1 := StrX( Item, "<a href=",1,9, """" ,1,1, T ) , Sub2 := StrX( Item, ">", T,1, "</a>",1,4 ) , Text .= UnHTM( Sub2 ) "`n" Sub1 "`n`n" MsgBox, %Text% ; Dependency :: Get UnHTM() www.autohotkey.com/forum/viewtopic.php?t=51342