Results 1 to 4 of 4

Thread: using gSub to edit a string in TTS?

  1. #1

    using gSub to edit a string in TTS?

    So I am trying to update my "mass import tool" and run into a small problem... I am trying to edit the string inputs .... but found a problem I can not work out how to fix...

    basically I want to "auto convert googledrive links"..

    So...
    Code:
    https://drive.google.com/file/d/0Bxv7nhmMwdKtRGo2LUlKSWJmTlE/view?usp=sharing
    becomes
    Code:
    https://drive.google.com/uc?export=download&id=0Bxv7nhmMwdKtRGo2LUlKSWJmTlE
    Now I thought this would be easy... this is my current test code...

    Code:
        local s = 'https://drive.google.com/file/d/0Bxv7nhmMwdKtRGo2LUlKSWJmTlE/view?usp=sharing'
        local r1 = 'https://drive.google.com/file/d/'
        local r2 = '/view?usp=sharing'
        print('1: '..s)
        s=s:gsub(r1, "*")
        s=s:gsub(r2, "+")
        print('2: '..s)
    The problem is that r2 will NOT edit.. no matter what I do... after much fiddeling I have discovered it is the "?" that is causing the problem...

    If I change r2 = /view or usp=sharing then the replace works.... but if I do... /view? then it only changed "/view" and if I put anything after the "?" like.. /view?usp it fails to find a match?

    Any ideas what is going on here?

    Here is a test code that may make it clear and the output..


    0 = original string, R = string to remove, N = new String

    Code:
        local s = '......../view?usp=sharing'
        local r2 = '/view?usp=sharing'
        local r3 = '/view'
        local r4 = '/view?usp'
        local r5 = '/view?'
        local r6 = 'usp=sharing'
        local r7 = '?usp=sharing'
    
        print('O : '..s)
        print('R : '..r2)
        local newS=s:gsub(r2, "")
        print('N : '..newS..'\n')
    
        print('O : '..s)
        print('R : '..r3)
        local newS=s:gsub(r3, "")
        print('N : '..newS..'\n')
    
        print('O : '..s)
        print('R : '..r4)
        local newS=s:gsub(r4, "")
        print('N : '..newS..'\n')
    
        print('O : '..s)
        print('R : '..r5)
        local newS=s:gsub(r5, "")
        print('N : '..newS..'\n')
    
        print('O : '..s)
        print('R : '..r6)
        local newS=s:gsub(r6, "")
        print('N : '..newS..'\n')
    
        print('O : '..s)
        print('R : '..r7)
        local newS=s:gsub(r7, "")
        print('N : '..newS..'\n')
    My Boardgame uTube chan - Tragic's Table Top
    BGG Guild of BoardGame uTubers - Tube Tables

  2. #2
    Why just gsub? There's match which fits this perfectly:
    Code:
    function ConvertLink(link)
        local id = link:match('/d/(.-)/') -- slash-d-slash-capture-slash
        return ('https://drive.google.com/uc?export=download&id=' .. id)
    end
    
    
    local link = 'https://drive.google.com/file/d/0Bxv7nhmMwdKtRGo2LUlKSWJmTlE/view?usp=sharing'
    local out = ConvertLink(link)
    print(out)
    If you want an example on how to use gsub, you could do it this way
    Code:
    local link = 'https://drive.google.com/file/d/0Bxv7nhmMwdKtRGo2LUlKSWJmTlE/view?usp=sharing'
    link = link:gsub('https://drive.google.com/file/d/', '') -- cut out the part before the ID
    link = link:gsub('/view%?usp=sharing', '') -- cut out the part after the ID, escape '?' char
    link = 'https://drive.google.com/uc?export=download&id=' .. link -- add the header to ID
    print(link)
    
    -- OR
    local link2 = 'https://drive.google.com/file/d/0Bxv7nhmMwdKtRGo2LUlKSWJmTlE/view?usp=sharing'
    link2 = link2:gsub('file/d/', 'uc?export=download&id=') -- replace the stuff from before ID directly with what you want
    link2 = link2:gsub('/view%?usp=sharing', '') -- cut out the unneded ending, escape '?' char
    print(link2)

  3. #3
    Quote Originally Posted by dzikakulka View Post
    Why just gsub? There's match which fits this perfectly:
    Code:
    function ConvertLink(link)
        local id = link:match('/d/(.-)/') -- slash-d-slash-capture-slash
        return ('https://drive.google.com/uc?export=download&id=' .. id)
    end
    
    
    local link = 'https://drive.google.com/file/d/0Bxv7nhmMwdKtRGo2LUlKSWJmTlE/view?usp=sharing'
    local out = ConvertLink(link)
    print(out)
    I so not understand that at all. Also never herd of "match".

    what dose "slash-d-slash-capture-slash" supposed to mean? Like how is that code finding stuff and more importantly replacing it with the correct stuff?

    Quote Originally Posted by dzikakulka View Post
    If you want an example on how to use gsub, you could do it this way
    Code:
    local link2 = 'https://drive.google.com/file/d/0Bxv7nhmMwdKtRGo2LUlKSWJmTlE/view?usp=sharing'
    link2 = link2:gsub('file/d/', 'uc?export=download&id=') -- replace the stuff from before ID directly with what you want
    link2 = link2:gsub('/view%?usp=sharing', '') -- cut out the unneded ending, escape '?' char
    print(link2)

    yeah this is what I was trying to do... so you put a "%" next to the "?".... why is that?
    Last edited by Tragic; 01-13-2018 at 01:39 PM.
    My Boardgame uTube chan - Tragic's Table Top
    BGG Guild of BoardGame uTubers - Tube Tables

  4. #4
    https://www.lua.org/manual/5.3/manual.html#6.4.1

    %x: (where x is any non-alphanumeric character) represents the character x. This is the standard way to escape the magic characters (...)
    These 5 or so pages explain how to use string package: https://www.lua.org/pil/20.html (an arrow at the bottom links to the next page)

Similar Threads

  1. Possible to spawn or edit DECKS in scripting?
    By Tragic in forum Scripting
    Replies: 2
    Last Post: 01-14-2018, 05:38 PM
  2. [ADDED] onPlayerChat(string message, string player_color)
    By JotaFaD in forum Scripting Suggestions
    Replies: 2
    Last Post: 08-25-2017, 05:45 PM
  3. How do you edit descriptions with scripting?
    By Tragic in forum Scripting
    Replies: 2
    Last Post: 04-15-2017, 10:21 PM
  4. Replies: 0
    Last Post: 06-06-2016, 12:03 PM
  5. Replies: 1
    Last Post: 04-28-2016, 05:48 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •