--!movie
--!encoding=utf-8
--****************************************************************************
-- @file Regular Expression Wrapper based on PRegEx Xtra
-- @author Valentin Schmidt
-- @version 0.2
--****************************************************************************
----------------------------------------
-- Counts the pattern matches in string.
-- Optional parameter matches (list) is filled with matches (indexed by back ref number)
--
-- @param {string} patt - The search pattern
-- @param {string} str - The input string
-- @param {list} [matches=void] - Optional, if specified, filled with matches
-- @param {bool} [caseInsensitive=false] - Optional, default = false = case-sensitive
-- @returns {number} Number of matches
----------------------------------------
on regex_search (patt, str, matches, caseInsensitive)
opts = "g"
if caseInsensitive then put "i" after opts
ret = PRegEx_Search([str], patt, opts)
if listP(matches) then
matches.add(PRegEx_GetMatchString())
n = PRegEx_GetMatchBRCount()
repeat with i = 1 to n
matches.add(PRegEx_GetMatchString(i))
end repeat
end if
return ret
end
----------------------------------------
-- Replaces all pattern matches with replacement string
--
-- @param {string} findPatt - The search pattern
-- @param {string} replStr - The replacement string
-- @param {string} str - The input string
-- @param {bool} [caseInsensitive=false] - Optional, default = false = case-sensitive
-- @returns {string} The new string
----------------------------------------
on regex_replace (findPatt, replStr, str, caseInsensitive)
opts = "g"
if caseInsensitive then put "i" after opts
tList = [str]
PRegEx_Replace(tList, findPatt, opts, replPatt)
return tList[1]
end
----------------------------------------
-- Replaces all pattern matches with replacement string according to "hash" (propertyList or Object)
-- Usage: put regexp_replace_hash("#(\d+)#", ["1":"X","2":"Y"], "aaa#1#bbb#2#ccc") -> "aaaXbbbYccc"
--
-- @param {string} patt - The search pattern
-- @param {propList|Object} hash
-- @param {string} str - The input string
-- @param {number} [num=0] - Index of back reference containing the index string
-- @param {bool} [caseInsensitive=false] - Optional, default = false = case-sensitive
-- @returns {string} The new string
----------------------------------------
on regexp_replace_hash (patt, hash, str, num, caseInsensitive)
if voidP(num) then num=0
opts = "g"
if caseInsensitive then put "i" after opts
tList = [str]
PRegEx_ReplaceExec(tList, patt, opts, #_replace_hash, [hash, num])
return tList[1]
end
-- replace_multi callback
on _replace_hash (hash, num)
s = PRegEx_GetMatchString(num)
n = hash.findPos(s)
if n>0 then return hash[n]
else return s
end
----------------------------------------
-- Replaces pattern matches in string with callback results based on match
-- Usage: put regexp_replace_callback("&#([0-9]+);", str, #_ord_to_utf8, me, 1) ->
--
-- @param {string} patt - The search pattern
-- @param {string} str - The input string
-- @param {symbol} cbHandler
-- @param {Object} [cbObject=_movie]
-- @param {number} [num=0] - Index of argument passed to callback
-- @param {bool} [caseInsensitive=false] - Optional, default = false = case-sensitive
-- @returns {string} The new string
----------------------------------------
on regexp_replace_callback (patt, str, cbHandler, cbObject, num, caseInsensitive)
if voidP(cbObject) then cbObject=_movie
if voidP(num) then num=0
opts = "g"
if caseInsensitive then put "i" after opts
l = [str]
PRegEx_ReplaceExec(l, patt, opts, #_replace_callback, [cbHandler, cbObject, num]) -- ig
return l[1]
end
-- replace callback
on _replace_callback (cbHandler, cbObject, num)
s = PRegEx_GetMatchString(num)
return call(cbHandler, cbObject, s)
end
----------------------------------------
-- Splits string based on RegExp pattern, returns list
--
-- @param {string} patt - The pattern used for splitting
-- @param {string} str - The input string
-- @param {bool} [caseInsensitive=false] - Optional, default = false = case-sensitive
-- @returns {list}
----------------------------------------
on regex_split (patt, str, caseInsensitive)
opts = "g"
if caseInsensitive then put "i" after opts
tList = [str]
return PRegEx_Split(tList, patt, opts)
end
--**************************************
-- String functions implemented via RegExp
--**************************************
----------------------------------------
-- Replaces all occurences of stringToFind with stringToInsert, returns new string
--
-- @param {string} stringToFind
-- @param {string} stringToInsert
-- @param {string} str - The input string
-- @param {bool} [caseInsensitive=false] - Optional, default = false = case-sensitive
-- @returns {string} The new string
----------------------------------------
on str_replace (stringToFind, stringToInsert, str, caseInsensitive)
opts = "g"
if caseInsensitive then put "i" after opts
tList = [str]
stringToFind = PRegEx_QuoteMeta(stringToFind)
stringToInsert = PRegEx_QuoteMeta(stringToInsert)
PRegEx_Replace(tList, stringToFind, opts, stringToInsert)
return tList[1]
end
----------------------------------------
-- Splits string based on delimter string, returns list
--
-- @param {string} delim - The string delimiter used for splitting
-- @param {string} str - The input string
-- @param {bool} [caseInsensitive=false] - Optional, default = false = case-sensitive
-- @returns {list}
----------------------------------------
on explode (delim, str, caseInsensitive)
opts = "g"
if caseInsensitive then put "i" after opts
tList = [str]
return PRegEx_Split(tList, PRegEx_QuoteMeta(delim), opts)
end
----------------------------------------
-- Returns number of occurences of string needle in string haystack
--
-- @param {string} haystack
-- @param {string} needle
-- @param {bool} [caseInsensitive=false] - Optional, default = false = case-sensitive
-- @returns {number}
----------------------------------------
on substr_count (haystack, needle, caseInsensitive)
opts = "g"
if caseInsensitive then put "i" after opts
cnt = PRegEx_Search([haystack], PRegEx_QuoteMeta(needle), opts)
if cnt>0 then return cnt
else return 0
end
----------------------------------------
-- Lower to upper case (only for characters in Latin-1 (ISO 8859-1) range)
--
-- @param {string} str - Input string
-- @returns {string} Result string
----------------------------------------
on strtoupper (str)
tList = [str&""] -- thanks to Alex da Franca for this trick, otherwise unwanted call by reference would happen!
PRegEx_Translate(tList, "[a-zäöü]", "[A-ZÄÖÜ]")
return tList[1]
end
----------------------------------------
-- Upper to lower case (only for characters in Latin-1 (ISO 8859-1) range)
--
-- @param {string} str - Input string
-- @returns {string} Result string
----------------------------------------
on strtolower (str)
tList = [str&""]
PRegEx_Translate(tList, "[A-ZÄÖÜ]", "[a-zäöü]")
return tList[1]
end