these patterns aren't regular expressions ^^
regular expressions are like 100 times more useful
also, shouldn't that function return arrays instead?
It returns multiple values for patterns with multiple captures.
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
Last edited by Jeremydeath; 06-30-2009 at 02:49 PM.
Jeremydeath's Wire Addons - Try Them... NOW!
these patterns aren't regular expressions ^^
regular expressions are like 100 times more useful
also, shouldn't that function return arrays instead?
It returns multiple values for patterns with multiple captures.
"It's easy to win forgiveness for being wrong; being right is what gets you into real trouble." - Bjarne Stroustrup
Lífið læðist lúmskt áfram
Yeah, I learned that only after I posted this... Oh well, at least LUA's patterns are more useful than just plain text searching.
As to the array, I didn't see any indication of this LUA function ever returning a table instead of a string, even when there are multiple matches.
Jeremydeath's Wire Addons - Try Them... NOW!
captures, not matches
string.match("hello world","(%S+)%s+(%S+)")
should return "hello" and "world"
"It's easy to win forgiveness for being wrong; being right is what gets you into real trouble." - Bjarne Stroustrup
Lífið læðist lúmskt áfram
Hmmm, that should return an array... but it doesn't in LUA. I think this is a LUA or Gmod bug.
This is the test code I used.
LUA Code:
function Capture_Test() local Matches = string.match("hello world","(%S+)%s+(%S+)") print(Matches) end timer.Create( "Capture_Test", 1, 0, Capture_Test)
It's in the autorun directory and it just prints:
"hello" to the console every second.
Jeremydeath's Wire Addons - Try Them... NOW!
yeah because you discarded the 2nd return value of string.match, which would be the 2nd capture.
lua_run print(string.match("hello world","(%S+)%s+(%S+)"))
print prints all of its arguments, tab-separated.
"It's easy to win forgiveness for being wrong; being right is what gets you into real trouble." - Bjarne Stroustrup
Lífið læðist lúmskt áfram
Ok, I see what you mean. I'll fix it up and re-post it when I'm done.
Jeremydeath's Wire Addons - Try Them... NOW!
Done.
It took me a while to figure out that string.match() returns multiple arguments, and not an array.
Here it is (also updated the first post):
Lua Code:
--Just wrapers for LUA's string.match --only uses Lua Patterns and those use "%" as an escape character, instead of "\" like regular expressions --see [url=http://wiki.garrysmod.com/?title=String.match]String.match - GMod Wiki[/url] for more explanation 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 return { string.match(InputString, PatternString, StartIndex) } end e2function array string:match(string PatternString ) local InputString = this return { string.match(InputString, PatternString) } end
Last edited by TomyLobo; 06-30-2009 at 06:14 PM. Reason: see next post
Jeremydeath's Wire Addons - Try Them... NOW!
I edited your code a bit.
strings shouldn't be returned from a function returning arrays
also, { <nothing> } is an empty array already, no need for a check.
E2 strings and numbers can never be nil too, so you don't need to check for that.
Also, maybe make versions for just returning one match, for simplicity?
the names i come up with are shit though:
matchOne
matchFirst
"It's easy to win forgiveness for being wrong; being right is what gets you into real trouble." - Bjarne Stroustrup
Lífið læðist lúmskt áfram
yeah, actually I was just thinking that it might cause errors if you return a string as an array...
I didn't know about the non-nil numbers/string thing. I'll have to remember that.
I was thinking about doing the find first match thing. It would be easy to implement. I'll make it and test it.
EDIT: Done and tested
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 return {string.match(InputString,PatternString,StartIndex)} end e2function array string:match(string PatternString ) local InputString = this return {string.match(InputString,PatternString)} end e2function string string:matchFirst( string PatternString , number StartIndex ) local InputString = this return string.match(InputString,PatternString,StartIndex) or "" end e2function string string:matchFirst(string PatternString ) local InputString = this return string.match(InputString,PatternString) or "" end
Last edited by Jeremydeath; 06-30-2009 at 07:16 PM.
Jeremydeath's Wire Addons - Try Them... NOW!
Bookmarks