Quantcast
WoWInterface - View Single Post - Pattern matching is my Kryptonite!
View Single Post
01-12-19, 11:43 AM   #1
myrroddin
A Pyroguard Emberseer
 
myrroddin's Avatar
AddOn Author - Click to view addons
Join Date: Oct 2008
Posts: 1,124
Pattern matching is my Kryptonite!

I am writing an external AddOn that converts invalid itemID lists into valid ones for TradeSkillMaster. However, for all that I have been reading on wowpedia.org and the official Lua documentation, I am stuck on matching exactly what I want in strings.

These are valid for TSM:
  • "i:82977"
  • "i:82977,i:10368"
While these are not valid:
  • "82977"
  • "82977,10368"
TSM requires itemIDs in the format "i:12345" and comma seperated if there are more than one. Okay, great. I can use string.gmatch (I don't care about what position the numbers are, so I'm not using string.match) like so:
Code:
local maybe_valid = string.gmatch(myString, "%d+$"
But that will match both examples above, valid and not valid. That won't work. How do I force the match to start at the first character of the string?

Checking if it is valid is easy, I think.
Code:
local valid = string.gmatch(myString, "i:%d+$"
The reason for knowing the difference is that after finding invalid strings, I will use string.gsub(myString, ",", ",i:") to fix them. Clearly I don't want to fix a string that is already valid.

Last edited by myrroddin : 01-12-19 at 11:47 AM. Reason: next step
  Reply With Quote