Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

[Solved]How can I save a string that you are replacing with string.gsub into a variable?

Asked by 6 years ago
Edited 6 years ago

I am currently working on a project that requires me to use string manipulation in order to accomplish the goals. One issue I have run into deals with finding certain items in a string while ignoring others. In an effort to do this I am implementing string.gsub() in order to replace the pattern I want to ignore with "substitute" or something similar. The issue is that I want to save what I am replacing with "substitute" for an operation in the future. I thought about using string.find() and string.match() but I don't know how well those will be able to work with the system I am implementing. Here is an example of what I am attempting to do. Input: "3x^2+2x^5-sec(3x+4x)" Parser result: "3x^2+2x^5-substitute" and have a variable that has the value of substitute. Here is my parser so far with no values being returned:

local function parseString(input)
    local s = input:gsub("%(([^()]+)%)", "substitute") -- here is what I don't know how to do
    local str -- this is if there is no symbol
    local firstString -- this is for before the symbol found
    local secondString -- this is for after the symbol found
    local plus = string.find(s, "+")
    local minus = string.find(s, "-")
    if plus and minus then
        if minus > plus then
            firstString = s:sub(1, plus)
            local nextPlus = string.find(s, "+", plus)
            local nextMinus = string.find(s, "-", plus)
            if nextPlus and nextMinus then
                if nextMinus > nextPlus then
                    secondString = s:sub(plus, nextPlus) 
                else
                    secondString = s:sub(plus, nextMinus) 
                end
            elseif nextPlus then
                secondString = s:sub(plus, nextPlus)
            elseif nextMinus then
                secondString = s:sub(plus, nextMinus)
            else
                secondString = s:sub(plus)      
            end
        else 
            firstString = s:sub(1, minus)       
            local nextPlus = string.find(s, "+", minus)
            local nextMinus = string.find(s, "-", minus)
            if nextPlus and nextMinus then
                if nextMinus > nextPlus then
                    secondString = s:sub(minus, nextPlus) 
                else
                    secondString = s:sub(minus, nextMinus) 
                end
            elseif nextPlus then
                secondString = s:sub(minus, nextPlus)
            elseif nextMinus then
                secondString = s:sub(minus, nextMinus)
            else
                secondString = s:sub(minus)     
            end
        end
    elseif plus then
        firstString = s:sub(1, plus)
        local nextPlus = string.find(s, "+", plus)
        if nextPlus then
            secondString = s:sub(plus, nextPlus)
        else 
            secondString = s:sub(plus)
        end
    elseif minus then
        firstString = s:sub(1, minus)
        local nextMinus = string.find(s, "-", minus)
        if nextMinus then
            secondString = s:sub(minus, nextMinus)
        else
            secondString = s:sub(minus)
        end     
    else
        str = s             
    end
end

I will deal with what the parser returns and how it recursively works later. I appreciate any help. Thanks!

Answer this question