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

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

  1. #1
    Wirererer Jeremydeath's Avatar
    Join Date
    Jun 2008
    Location
    Texas, USA
    Posts
    383

    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:
    [highlight=LUA]--Just wrapers for LUA's string.match
    --only uses LUA Patterns and those use "%" instead of "\" like regular expressions
    --see String.match - GMod Wiki 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[/highlight]

    And a quick use of it would be to get a number from a string.
    [highlight=E2]@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
    [/highlight]
    Last edited by Jeremydeath; 06-30-2009 at 03:49 PM.
    Jeremydeath's Wire Addons - Try Them... NOW!

  2. #2
    Bug Buster TomyLobo's Avatar
    Join Date
    Feb 2009
    Posts
    2,796

    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
    Wirererer Jeremydeath's Avatar
    Join Date
    Jun 2008
    Location
    Texas, USA
    Posts
    383

    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's Avatar
    Join Date
    Feb 2009
    Posts
    2,796

    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
    Wirererer Jeremydeath's Avatar
    Join Date
    Jun 2008
    Location
    Texas, USA
    Posts
    383

    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.
    [highlight=LUA]
    function Capture_Test()
    local Matches = string.match("hello world","(%S+)%s+(%S+)")
    print(Matches)
    end
    timer.Create( "Capture_Test", 1, 0, Capture_Test)
    [/highlight]

    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's Avatar
    Join Date
    Feb 2009
    Posts
    2,796

    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
    Wirererer Jeremydeath's Avatar
    Join Date
    Jun 2008
    Location
    Texas, USA
    Posts
    383

    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
    Wirererer Jeremydeath's Avatar
    Join Date
    Jun 2008
    Location
    Texas, USA
    Posts
    383

    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):
    [highlight=Lua]--Just wrapers for LUA's string.match
    --only uses Lua Patterns and those use "%" as an escape character, instead of "\" like regular expressions
    --see String.match - GMod Wiki 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
    [/highlight]
    Last edited by TomyLobo; 06-30-2009 at 07:14 PM. Reason: see next post
    Jeremydeath's Wire Addons - Try Them... NOW!

  9. #9
    Bug Buster TomyLobo's Avatar
    Join Date
    Feb 2009
    Posts
    2,796

    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&#237;fi&#240; l&#230;&#240;ist l&#250;mskt &#225;fram

  10. #10
    Wirererer Jeremydeath's Avatar
    Join Date
    Jun 2008
    Location
    Texas, USA
    Posts
    383

    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

    [highlight=LUA]--Just wrapers for LUA's string.match
    --only uses LUA Patterns and those use "%" instead of "\" like regular expressions
    --see String.match - GMod Wiki 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[/highlight]
    Last edited by Jeremydeath; 06-30-2009 at 08:16 PM.
    Jeremydeath's Wire Addons - Try Them... NOW!

+ Reply to Thread
Page 1 of 2 12 LastLast

Similar Threads

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