Now, you will learn the use of regular expressions in AutoIt. You get string filters to collect or store filtered data from long text strings as Internet pages, documents, or databases.
Open SciTE from the AutoIt Program Group and start writing source code.
You have to learn about Regular Expression Syntax. It is a very common subject in other programming languages, such as PHP, Python, and others. See more at SciTE Function Help: String Reg Exp Syntax (http://www.autoitscript.com/autoit3/docs/functions/StringRegExp.htm).
The general RegExp
function syntax is:
StringRegExp ("test", "pattern" [, flag [, offset]])
You can either write, or copy and paste to SciTE editor the following codes:
;Example 1 (Basic) Search String Occurrences inside another string. $target = "Cat is lying on the floor." $myString = "o" $ResultsArray = StringRegExp ($target, $myString, 3) Msgbox (0, "Count", "String" & $myString & "': " & Ubound ($ResultsArray) & " Occurrences inside " & $target & "' target String.")
;Example 2 (Advanced) Search urls ; $st2 Target String with some Urls ; _ join several lines together $st2='<a href="http://www.google.com">' & _ '<a href="http://www.link1.com"> http://www.link2.com ' & _ 'link http://www.link3.com' $pattern1="(?si)(?:.*?)?(https?://[w.:]+/?(?:[w/?&=.~;-+!*_#%])*)" $array = StringRegExp($st2,$pattern1,3) ;we get an array with Results $links="" for $a=0 to Ubound($array)-1 $links=$links & $array[$a] & @CRLF Next ; Links Result String. Msgbox(0,"Links","Results: " & @CRLF & $links )
Press F5 to run the script.
In the first example code, we defined a text string to be searched for, $target
, and a pattern called $mystring
.
StringRegExp
function's parameter 3
will get an array back, that is, $ResultsArray
.
The Msgbox
function displays a number of occurrences for this pattern inside the target string, and Ubound ($ResultsArray)
returns the size of the results array.
In the second example code, we defined a text string to be searched for called $st2
, and a pattern called $pattern1
.
StringRegExp
function's parameter 3
will return an array, that is, $array
.
The For
...Next
function loops through all values inside the results array, and it creates a new result string: Results found.
The Msgbox
function displays the number of occurrences of this pattern inside the search string obtained from Ubound ($array)
.