These are just some quick little E2 wrappers for LUA's string.match() function. Pretty easy stuff, but I was surprised that this was not already in the SVN. Useful because the S:find(S) function only return the beginning position of the string, not very useful if you want the actual thing that it found.
Here it is:
LUA Code:
--Just wrapers for LUA's string.match
--only uses LUA Patterns and those use "%" instead of "\" like regular expressions
--see [url=http://wiki.garrysmod.com/?title=String.match]String.match - GMod Wiki[/url] for more explanarion on the function and
--http://lua-users.org/wiki/PatternsTutorial for more info on patterns
--made by Jeremydeath, 6-30-09
e2function array string:match( string PatternString , number StartIndex )
local InputString = this
if(!InputString or !PatternString or !StartIndex) then return "" end
local Ret = {string.match(InputString,PatternString,StartIndex)}
if(Ret) then
return Ret
else
return ""
end
end
e2function array string:match(string PatternString )
local InputString = this
if(!InputString or !PatternString) then return "" end
local Ret = {string.match(InputString,PatternString)}
if(Ret) then
return Ret
else
return ""
end
end
And a quick use of it would be to get a number from a string.
E2 Code:
@name match test
@inputs
@outputs Number
@persist
@trigger all
String = "Hello, my name is Jeremydeath and I like the number pi which is approximately 3.141592653589, which is the ratio between the diameter and circumference of a circle."
Number = String:match("[%d%.]+"):toNumber()
#Number = 3.141592653589
Number = String:match("[%d%.]+" , 84):toNumber()
#Number = 592653589
Bookmarks