The function idea came from the function I launched for NSIS with the same name.
Let's see if this beaulty has any bugs... Any bug reports are accepted. Feature requests and patches will be selected first, then added depending on the usage.
(What's really painful is not even doing the function, is posting scripts directly on the page, because you have to format the whole script to fit in 60 characters length. I suggest you to add an option to attach files to pages.)
; AutoHotkey Version: 1.x ; Language: All ; Platform: Win9x/NT ; Author: Diego Pedroso (deguix) ; ; Advanced Search in String Function (AdvStrStr): ;------------------------------------------------------------ ; ; This function brings to you from C language a function ; called StrStr with advanced options for those who want to ; expand the simple IfInString function from AutoHotkey into ; a powerful string searcher. ; ; How to use it: ;------------------------------------------------------------ ; ; 1) Insert or include at the end of your script the part ; below "Function AdvStrStr" (see it after the function ; example below). ; ; 2) Set the input variables below where you want to use the ; function: ; ; (input) (required) AdvStrStr_String = The string where do ; you want to search for ; "AdvStrStr_StrToSearch". ; ; (input) (required) AdvStrStr_StrToSearch = A string to find ; on "AdvStrStr_String". ; ; (input) AdvStrStr_DirectionOfSearch = (>|<) The direction ; where the search is going to (> = foward, < = ; backwards). Default is ">" (foward). ; ; (input) AdvStrStr_DirectionOfReturn = (>|<) Return the ; string to the left or to the right of ; "AdvStrStr_StrToSearch" (> = right, < = left). ; Default is ">" (right). ; ; (input) AdvStrStr_Loops = Number of times ; "AdvStrStr_StrToSearch" has to be found to return ; the result. "0" is one time, "1" is two times... ; Default is "0" (just one time). ; ; (input) AdvStrStr_ShowStrToSearch = (1|0) If "1", ; "ErrorLevel" will return a string with ; "AdvStrStr_StrToSearch" included. Default is "1". ; ; Auto Hotkey commands that affect this function: ; ; StringCaseSense, (on|off) ; ; 3) Call the function by using "Gosub, AdvStrStr". ; ; 4) Use the output variables below: ; ; (output) ErrorLevel = (Result|1|2) If ; "AdvStrStr_StrToSearch" was found on ; "AdvStrStr_String", "ErrorLevel" is the string ; found depending on user options. If it didn't ; find "AdvStrStr_StrToSearch" on ; "AdvStrStr_String", "ErrorLevel" will return "1". ; If "AdvStrStr_StrToSearch" is not set, ; "ErrorLevel" will return "2". ; ; (output) AdvStrStr_StartCharPos = Position of the first ; "AdvStrStr_StrToSearch" character. It's blank when ; "AdvStrStr_StrToSearch" isn't found ("1" = ; "AdvStrStr_String" first character, "2" = ; second...). ; ; (output) AdvStrStr_EndCharPos = Position of the last ; "AdvStrStr_StrToSearch" character. It's blank when ; "AdvStrStr_StrToSearch" isn't found. ("1" = ; "AdvStrStr_String" first character, "2" = ; second...) ; ; (output) AdvStrStr_Temp = Temporary string used for ; comparasions with "AdvStrStr_StrToSearch". This ; variable will ever give the same value as ; "AdvStrStr_StrToSearch" except when it's not ; found. In this case, this value will be blank. ; ; Credits: ;------------------------------------------------------------ ; ; Advanced Search in String AutoHotkey and NSIS versions ; created by Diego Pedroso (deguix). ; ;------------------------------------------------------------ ; THIS BELOW IS A FUNCTION EXAMPLE: ; ; This function doesn't require all it's variables for ; the correct function use. This has been heavily tested ; against problems, so you could even try to don't use ; the required variables by this function (but yeah, it ; would return an error value of 1 or 2). ; AdvStrStr_String = This is just a test AdvStrStr_StrToSearch = is AdvStrStr_DirectionOfSearch = < AdvStrStr_DirectionOfReturn = > AdvStrStr_Loops = 0 AdvStrStr_ShowStrToSearch = 1 StringCaseSense, on Gosub, AdvStrStr if ErrorLevel = 1 MsgBox,64, Advanced Search in String function, Cannot find: "%AdvStrStr_StrToSearch%" else if ErrorLevel = 2 MsgBox,16, Advanced Search in String function, Error: "AdvStrStr_StrToSearch" variable was not set before function call. else MsgBox,64, Advanced Search in String function, Return: "%ErrorLevel%" Exit ; END OF FUNCTION EXAMPLE ;------------------------------------------------------------ ;------------------------------------------------------------ ; Function AdvStrStr ;------------------------------------------------------------ AdvStrStr: ; Verify if we have the correct values on the variables if AdvStrStr_String = { ErrorLevel = 1 ;AdvStrStr_StrToSearch not found Goto AdvStrStr_End } if AdvStrStr_StrToSearch = { ErrorLevel = 2 ;No text to search Goto AdvStrStr_End } if AdvStrStr_DirectionOfReturn <> < AdvStrStr_DirectionOfReturn = > if AdvStrStr_DirectionOfSearch <> < AdvStrStr_DirectionOfSearch = > if AdvStrStr_Loops <= 0 AdvStrStr_Loops = 0 if AdvStrStr_ShowStrToSearch <> 0 AdvStrStr_ShowStrToSearch = 1 ; Find "AdvStrStr_String" length StringLen, AdvStrStr_StringLen, AdvStrStr_String ; Then find "StrToSeach" length StringLen, AdvStrStr_StrToSearchLen, AdvStrStr_StrToSearch ; Now set up basic variables if AdvStrStr_DirectionOfSearch = < { AdvStrStr_StartCharPos = %AdvStrStr_StringLen% AdvStrStr_StartCharPos -= %AdvStrStr_StrToSearchLen% AdvStrStr_EndCharPos = %AdvStrStr_StringLen% } else { AdvStrStr_StartCharPos = 0 AdvStrStr_EndCharPos = %AdvStrStr_StrToSearchLen% } AdvStrStr_Loop = 0 ; First loop ;Let's begin the search Loop { ; Step 1: If the starting or ending numbers are negative ; or more than AdvStrStr_StringLen, we return ; error IfLess, AdvStrStr_StartCharPos, 0 { AdvStrStr_StartCharPos = AdvStrStr_EndCharPos = AdvStrStr_Temp = ErrorLevel = 1 ;AdvStrStr_StrToSearch not found Goto AdvStrStr_End } IfGreater, AdvStrStr_EndCharPos, %AdvStrStr_StringLen% { AdvStrStr_StartCharPos = AdvStrStr_EndCharPos = AdvStrStr_Temp = ErrorLevel = 1 ;AdvStrStr_StrToSearch not found Goto AdvStrStr_End } ; Step 2: Start the search depending on ; AdvStrStr_DirectionOfSearch. Chop down not needed ; characters. If AdvStrStr_StartCharPos <> 0 StringTrimLeft, AdvStrStr_Temp, AdvStrStr_String, %AdvStrStr_StartCharPos% If AdvStrStr_EndCharPos <> %AdvStrStr_StringLen% If AdvStrStr_StartCharPos = 0 StringLeft, AdvStrStr_Temp, AdvStrStr_String, %AdvStrStr_StrToSearchLen% else StringLeft, AdvStrStr_Temp, AdvStrStr_Temp, %AdvStrStr_StrToSearchLen% ; Step 3: Make sure that's the string we want If AdvStrStr_Temp = %AdvStrStr_StrToSearch% { ;We found it, return except if the user has set up to ;search for another one: If AdvStrStr_Loop >= %AdvStrStr_Loops% { ;Now, let's see if the user wants ;AdvStrStr_StrToSearch to appear: If AdvStrStr_ShowStrToSearch = 0 { ;Return depends on AdvStrStr_DirectionOfReturn If AdvStrStr_DirectionOfReturn = < ; RTL StringLeft, ErrorLevel, AdvStrStr_String, %AdvStrStr_StartCharPos% else ; LTR StringTrimLeft, ErrorLevel, AdvStrStr_String, %AdvStrStr_EndCharPos% Goto AdvStrStr_End } else { ;Return depends on AdvStrStr_DirectionOfReturn If AdvStrStr_DirectionOfReturn = < ; RTL StringLeft, ErrorLevel, AdvStrStr_String, %AdvStrStr_EndCharPos% else ; LTR StringTrimLeft, ErrorLevel, AdvStrStr_String, %AdvStrStr_StartCharPos% Goto AdvStrStr_End } } else { ;If the user wants to have more loops, let's do it so! AdvStrStr_Loop += 1 if AdvStrStr_DirectionOfSearch = < { AdvStrStr_StartCharPos -= 1 AdvStrStr_EndCharPos -= 1 } else { AdvStrStr_StartCharPos += 1 AdvStrStr_EndCharPos += 1 } } } else { ; Step 4: We didn't find it, so do steps 1 thru 3 again if AdvStrStr_DirectionOfSearch = < { AdvStrStr_StartCharPos -= 1 AdvStrStr_EndCharPos -= 1 } else { AdvStrStr_StartCharPos += 1 AdvStrStr_EndCharPos += 1 } } } AdvStrStr_End: ;Add 1 to AdvStrStr_StartCharPos because "StrTrimLeft" was ;used before AdvStrStr_StartCharPos += 1 Return