-1

So I have a string in Lua that is exactly like the following:

[[
  en[What is your job]
  pt[Qual é o seu trabalho?]
]]

As you might expect, I want to sometimes retrieve anything inside "en[]" and in other times "pt[]" and put the text inside a new variable.

Any ideas?

Alexandre Severino
  • 1,563
  • 1
  • 16
  • 38

1 Answers1

3

Edit: Improved speed by removing some concatenation.

This should work:

    String = [[
        en[What is your job]
        pt[Qual é o seu trabalho?]
    ]]

    function RetrieveElementFromString( String, Element, ContainerOpen, ContainerClose ) -- String is the "[[]]" part.
        return String:match( Element .. ContainerOpen .. '(.-)' .. ContainerClose )
    end

    print( RetrieveElementFromString( String, 'en', '%[', '%]' ) ) -- > What is your job
DavisDude
  • 902
  • 12
  • 22
  • Didn't work here... what I tried is: `localeMessage = string.find([[ en[Hello!] ]], 'en%[(.-[^%]])%]')` `print(localeMessage)` outputs "2" Edit: changed to string:match and got what I was looking for. Thanks! – Alexandre Severino Jun 02 '14 at 04:21
  • Ah, yes. My bad. `string.find` would have returned it as the third argument, the first two being the start and end (respectively) of the string. – DavisDude Jun 02 '14 at 10:16
  • Hey, I was thinking about it and realized that I had an extraneous concatenation. Now it is shorter, using less concatenation, making it faster. – DavisDude Jun 03 '14 at 01:01