WoW:USERAPI strfindt: Difference between revisions

From AddOn Studio
Jump to navigation Jump to search
No edit summary
 
m (Move page script moved page USERAPI strfindt to USERAPI strfindt without leaving a redirect)
 
(One intermediate revision by one other user not shown)
Line 55: Line 55:


__NOTOC__
__NOTOC__
[[Category:User Defined Functions]]
[[Category:User defined functions]]

Latest revision as of 04:49, 15 August 2023

This page documents a <i>user-defined function</i> that you can copy and paste into your addon. Replace PREFIX with your addon or lib prefix to avoid conflicts between different versions of these functions.

User defined functions

strfindt - by Mikk -


A thin wrapper for strfind that returns the results as a table, allowing it to be used in e.g. if() clauses while capturing subexpressions.

nBeginPos, nEndPos = <PREFIX>_strfindt(tableCaptures, strHaystack, strRegex, nStartPos, boolPlain)


Function Parameters[edit]

Arguments[edit]

(tableCaptures, strHaystack, strRegex, nStartPos, boolPlain)
tableCaptures
output Table - captured subexpressions

(identical to strfind() after this point)

strHaystack
String - the string to perform the search in
strRegex
String - the regular expression (search pattern)
nStartPos
Number - the position to begin the search on (nil = from the beginning)
boolPlain
Boolean - set to true to not use regexes (which defeats the whole purpose of this wrapper)

Returns[edit]

nBegin, nEnd
nBeginPos
Number - beginning of match (1-based) or nil for no match
nEndPos
String - end position of match


Example[edit]

 local results = {};
 if(<PREFIX>_strfindt(results, 
   '<b><a href="wowwiki.com">WoWWiki</a>', 
   '<a [^>]*%fhref="([^"]*)"[^>]>([^<])+</a>')
 then
   print results[2] . " lives at " . results[1];
 end

Result[edit]

 WoWWiki lives at wowwiki.com

Code[edit]

 -- Returns: nBeginPos, nEndPos.  Captured expressions go in tOut
 function <PREFIX>_strfindt(tOut, strFind, strRegex, nStart, bPlain)
   local a = { string.find(strFind, strRegex, nStart, bPlain) };
   while(getn(tOut)>0) do tremove(tOut,1); end
   for _,v in a do
     tinsert(tOut, v);
   end
   return a[1], a[2];
 end