Since regular expressions are used a lot in triggers I made a script to help with testing patterns:
------------------------------
-- regex test --
------------------------------
function retest(regexPattern, stringToTest)
local col="<orange>"
retestMatches = nil
local tt = tempRegexTrigger(regexPattern, [[retestMatches=matches]])
cecho(col.."Regex test\n---------------------\n")
cecho(col.."pattern:\n<white>" .. regexPattern )
feedTriggers("\n")
cecho(col.."string:")
feedTriggers( stringToTest .. "\n")
killTrigger(tt)
echo("\n")
if(retestMatches) then
for k,v in ipairs(retestMatches) do
cecho(col.."matches["..k.."]: <white>" .. v .. "\n")
end
else
echo("String did NOT match the pattern.\n\n")
end
retestMatches = nil
end
Just add it to a new script and save it, then you can test patterns from the command line like this:
lua retest( <pattern>, <string to test>)
example:
lua retest([[^\*\* (\d+)/(\d+) HP (\d+)/.*]], [[** 123/234 HP 44/88 SP blahblah]])
output:
Code: Select all
Regex test
---------------------
pattern:
^\*\* (\d+)/(\d+) HP (\d+)/.*
string:
** 123/234 HP 44/88 SP blahblah
matches[1]: ** 123/234 HP 44/88 SP blahblah
matches[2]: 123
matches[3]: 234
matches[4]: 44
When using it, I reccomend that you encase the strings with [[ ]] instead of quotes, otherwise you need to use \\ instead of \ everywhere in the pattern.
You can also call the function within your scripts, that may be easier to read.
Again, set the values using [[ ]] in that case:
Code: Select all
local p = [[^(\w+) tells you: (.*)$]]
local s = [[NameHere tells you: Hello there!]]
retest(p,s)
...output...
matches[1]: NameHere tells you: Hello there!
matches[2]: NameHere
matches[3]: Hello there!
Let me know if you have any questions or suggestions on improvements.