2

This topic has been partially handled before by another user in thread: Lua string.gsub with Multiple Patterns

I'm having issues and I believe it is with my pattern (second) argument. Here is my example of the gsub code I'm trying to use:

local dateCode = "MMM/dd-YYYY:ss"

--dateCode = dateCode:gsub(".*", {["%/"] = "s", ["%-"] = "n", ["%:"] = "c"}) --original code but removed after @Etan's comments.
dateCode = dateCode:gsub(".*", {["/"] = "s", ["-"] = "n", [":"] = "c"})

print(dateCode)

MMM/dd-YYYY:ss  --printed

MMMsddnYYYYcss  --desired

I believe that I shouldn't be looking over all characters like I currently have it, but I'm not sure what pattern I should be using for the dateCode variable. The idea is to replace the keys with the first alpha character that it begins with.

6
  • What is your desired output here? MMMsddnYYYYcss? The keys in the gsub replacement need to equal the captured bits of the string that the pattern matches. You don't have % in your input so none of your keys can possibly match. Commented Nov 13, 2015 at 20:00
  • @EtanReisner you are correct. That is the desired output. I erroneously used the % as escape characters in my keys. Thanks. Once removed, still does not appear to provide the desired result. Commented Nov 13, 2015 at 20:05
  • Think about what you are matching with your pattern. What will the "result" of the match be? That is what gsub looks up in the table argument. So if you want to replace just those characters then you need to, individually, match just those characters. Commented Nov 13, 2015 at 20:10
  • @EtanReisner UGHH..... thank you for spelling it out of me. %p...... I'll update my post to show the correct pattern. Commented Nov 13, 2015 at 20:19
  • Don't update the post. If you have a solution add an answer and accept it. Commented Nov 13, 2015 at 20:22

1 Answer 1

3

Since you want a select set of characters to be replaces, put them in a character set as the pattern:

dateCode = dateCode:gsub("[/:-]", {["/"] = "s", ["-"] = "n", [":"] = "c"})

What happens currently is, with the pattern .* in place, it matches the entire string. Since the string "MMM/dd-YYYY:ss" has no indexed value in the hash table (second argument), no replacement actually occurs.

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

2 Comments

Thanks hjpotter. I used the following dateCode:gsub("%p", {["/"] = "s", ["-"] = "n", [":"] = "c"}) and it worked. What would be the consequences of doing it my way over the way you just posted? Ahhh... I get your explanation!!! Thank you.
%p covers a lot more characters. It'll also include characters such as +, ! etc. While there'll be no effect on the performance (or output), I'd suggest using a harder limit in your gsub pattern. If you're sure about what you want, use specifically that.

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.