1

I have a phrase, where only some words will change, and I need to store those words on a variable.

Example:

phrase = "I cannot connect to server XPTO\TEST for the last five hours"

The only part that will change is XPTO\TEST and I need to store it on a variable so that I can use it later.

Any ideas, or is it possible?

2
  • 1
    I don't understand the question. You are having problem on what, how to extract XPTO\TEST from the phrase? How do you want to use the variable later? Commented Aug 13, 2015 at 15:21
  • 1
    Try servername = phrase:match'server%s+(%S+)' Commented Aug 13, 2015 at 17:50

1 Answer 1

2

Seems like you need some form of placeholders, if that is a case, then you can use string.format or string.gsub.

 local t = {name="lua", version="5.3"}
 x = string.gsub("$name-$version.tar.gz", "%$(%w+)", t)
 --> x="lua-5.3.tar.gz"

With PHP for example you can achieve what you want without any extra work done, because there is a feature called string interpolation (wiki).

But at the same time Lua doesn't have one, that's why you can't do that without extra string post-processing.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.