+ Reply to Thread
Page 1 of 2 1 2 LastLast
Results 1 to 10 of 15

Thread: E2 string match functions (for LUA's string.Match functions)

  1. #1
    Patcherer... er
    Jeremydeath will become famous soon enough Jeremydeath's Avatar
    Join Date
    Jun 2008
    Location
    Texas, USA
    Posts
    374

    Post E2 string match functions (for LUA's string.Match functions)

    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:
    1. --Just wrapers for LUA's string.match
    2. --only uses LUA Patterns and those use "%" instead of "\" like regular expressions
    3. --see [url=http://wiki.garrysmod.com/?title=String.match]String.match - GMod Wiki[/url] for more explanarion on the function and
    4. --http://lua-users.org/wiki/PatternsTutorial for more info on patterns
    5. --made by Jeremydeath, 6-30-09
    6.  
    7. e2function array string:match( string PatternString , number StartIndex )
    8. local InputString = this
    9. if(!InputString or !PatternString or !StartIndex) then return "" end
    10. local Ret = {string.match(InputString,PatternString,StartIndex)}
    11. if(Ret) then
    12. return Ret
    13. else
    14. return ""
    15. end
    16. end
    17.  
    18. e2function array string:match(string PatternString )
    19. local InputString = this
    20. if(!InputString or !PatternString) then return "" end
    21. local Ret = {string.match(InputString,PatternString)}
    22. if(Ret) then
    23. return Ret
    24. else
    25. return ""
    26. end
    27. end

    And a quick use of it would be to get a number from a string.
    E2 Code:
    1. @name match test
    2. @inputs
    3. @outputs Number
    4. @persist
    5. @trigger all
    6.  
    7. 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."
    8.  
    9. Number = String:match("[%d%.]+"):toNumber()
    10. #Number = 3.141592653589
    11.  
    12. Number = String:match("[%d%.]+" , 84):toNumber()
    13. #Number = 592653589
    Last edited by Jeremydeath; 06-30-2009 at 02:49 PM.
    Jeremydeath's Wire Addons - Try Them... NOW!

  2. #2
    Bug Buster

    TomyLobo has a spectacular aura about TomyLobo has a spectacular aura about TomyLobo's Avatar
    Join Date
    Feb 2009
    Posts
    2,786

    Default Re: E2 string match functions (for LUA's string.Match functions)

    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

  3. #3
    Patcherer... er
    Jeremydeath will become famous soon enough Jeremydeath's Avatar
    Join Date
    Jun 2008
    Location
    Texas, USA
    Posts
    374

    Default Re: E2 string match functions (for LUA's string.Match functions)

    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!

  4. #4
    Bug Buster

    TomyLobo has a spectacular aura about TomyLobo has a spectacular aura about TomyLobo's Avatar
    Join Date
    Feb 2009
    Posts
    2,786

    Default Re: E2 string match functions (for LUA's string.Match functions)

    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

  5. #5
    Patcherer... er
    Jeremydeath will become famous soon enough Jeremydeath's Avatar
    Join Date
    Jun 2008
    Location
    Texas, USA
    Posts
    374

    Default Re: E2 string match functions (for LUA's string.Match functions)

    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:
    1. function Capture_Test()
    2. local Matches = string.match("hello world","(%S+)%s+(%S+)")
    3. print(Matches)
    4. end
    5. 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!

  6. #6
    Bug Buster

    TomyLobo has a spectacular aura about TomyLobo has a spectacular aura about TomyLobo's Avatar
    Join Date
    Feb 2009
    Posts
    2,786

    Default Re: E2 string match functions (for LUA's string.Match functions)

    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

  7. #7
    Patcherer... er
    Jeremydeath will become famous soon enough Jeremydeath's Avatar
    Join Date
    Jun 2008
    Location
    Texas, USA
    Posts
    374

    Default Re: E2 string match functions (for LUA's string.Match functions)

    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!

  8. #8
    Patcherer... er
    Jeremydeath will become famous soon enough Jeremydeath's Avatar
    Join Date
    Jun 2008
    Location
    Texas, USA
    Posts
    374

    Default Re: E2 string match functions (for LUA's string.Match functions)

    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:
    1. --Just wrapers for LUA's string.match
    2. --only uses Lua Patterns and those use "%" as an escape character, instead of "\" like regular expressions
    3. --see [url=http://wiki.garrysmod.com/?title=String.match]String.match - GMod Wiki[/url] for more explanation on the function and
    4. --http://lua-users.org/wiki/PatternsTutorial for more info on patterns
    5. --made by Jeremydeath, 6-30-09
    6.  
    7. e2function array string:match( string PatternString, number StartIndex )
    8. local InputString = this
    9. return { string.match(InputString, PatternString, StartIndex) }
    10. end
    11.  
    12. e2function array string:match(string PatternString )
    13. local InputString = this
    14. return { string.match(InputString, PatternString) }
    15. end
    Last edited by TomyLobo; 06-30-2009 at 06:14 PM. Reason: see next post
    Jeremydeath's Wire Addons - Try Them... NOW!

  9. #9
    Bug Buster

    TomyLobo has a spectacular aura about TomyLobo has a spectacular aura about TomyLobo's Avatar
    Join Date
    Feb 2009
    Posts
    2,786

    Default Re: E2 string match functions (for LUA's string.Match functions)

    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

  10. #10
    Patcherer... er
    Jeremydeath will become famous soon enough Jeremydeath's Avatar
    Join Date
    Jun 2008
    Location
    Texas, USA
    Posts
    374

    Default Re: E2 string match functions (for LUA's string.Match functions)

    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:
    1. --Just wrapers for LUA's string.match
    2. --only uses LUA Patterns and those use "%" instead of "\" like regular expressions
    3. --see [url=http://wiki.garrysmod.com/?title=String.match]String.match - GMod Wiki[/url] for more explanarion on the function and
    4. --http://lua-users.org/wiki/PatternsTutorial for more info on patterns
    5. --made by Jeremydeath, 6-30-09
    6.  
    7. e2function array string:match( string PatternString , number StartIndex )
    8. local InputString = this
    9. return {string.match(InputString,PatternString,StartIndex)}
    10. end
    11.  
    12. e2function array string:match(string PatternString )
    13. local InputString = this
    14. return {string.match(InputString,PatternString)}
    15. end
    16.  
    17. e2function string string:matchFirst( string PatternString , number StartIndex )
    18. local InputString = this
    19. return string.match(InputString,PatternString,StartIndex) or ""
    20. end
    21.  
    22. e2function string string:matchFirst(string PatternString )
    23. local InputString = this
    24. return string.match(InputString,PatternString) or ""
    25. end
    Last edited by Jeremydeath; 06-30-2009 at 07:16 PM.
    Jeremydeath's Wire Addons - Try Them... NOW!

+ Reply to Thread
Page 1 of 2 1 2 LastLast

Similar Threads

  1. NPC functions - Egate2
    By Bobsymalone in forum Wiremod Addons
    Replies: 50
    Last Post: 10-30-2009, 08:15 AM
  2. functions
    By bobthe2lol in forum Expression 1 & 2
    Replies: 2
    Last Post: 06-24-2009, 08:22 AM
  3. E2 - CJ's functions
    By [FX]CJ[CrS] in forum Wiremod Addons
    Replies: 5
    Last Post: 04-22-2009, 11:36 AM
  4. E-2 some functions.
    By Whodunnit in forum Ideas & Suggestions
    Replies: 12
    Last Post: 03-11-2009, 04:31 AM
  5. E2: Few new functions
    By d3cr1pt0r in forum Wiremod Lua Coding
    Replies: 24
    Last Post: 02-22-2009, 09:17 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
proceed-collector
proceed-collector
proceed-collector
proceed-collector
linguistic-parrots
linguistic-parrots
linguistic-parrots
linguistic-parrots