stringluapattern-matchingmatchdata-manipulation

Weird behaviour in Lua string.gmatch


I would need to extract everything that comes before and everything that comes after the "\\scalebox" string. I tried with

local example = "sometext\\scalebox{0.74}"
for i in string.gmatch(example, "[^\\scalebox]+") do
  print(i)
end

I expect

sometext
{0.74}

but the result is

m
t
t
{0.74}

EDIT: not an escape chatacter problem, because

local example = "sometextscalebox{0.74}"
for i in string.gmatch(example, "[^scalebox]+") do
  print(i)
end

leads to same result

Any ideas?


Solution

  • I'm assuming you've seen other posts that tell you to use [^sep] to split a string by the sep character. These work, but only for one character long separators.

    So for your pattern, [^\\scalebox]+, we will split on ALL the characters \,s,c,a,l,e,b,o, or x. Which results in the list of strings that you're getting: "m", "t", "t", "{0.74}".

    Since you want to split on text, rather than characters, you can use string.find to find the index of your "\scalebox", and then string.sub to substring around that index:

    local s = "sometext\\scalebox{0.74}"
    local pattern = "\\scalebox"
    
    local index = s:find(pattern)
    local first = s:sub(1, index - 1)
    local second = s:sub(index + string.len(pattern), string.len(s))
    

    This obviously can be made into a loop and be made to output as a table as needed, but you only mentioned needing the first/second halves.