#String Matching!
I think the best option here is to use some simple string matching!
It's pretty easy with some basic patterns:
str = "[0,1,40,40,348,348,348,348,348,348]"
for match in string.gmatch(str, "[^%[%],%s]+" ) do
print(match)
end
If you run the code, you'll see that only the numbers are printed.
Allow me to explain the pattern:
#Complement
Whenever you have something like this, [^ ], that is called a complement. Anything within the brackets that is after the ^ will be opposed. By this, I mean, the compiler will match everything isn't specified in the complement.
#%[ and %]
Since we see [ and ] in your string, we certainly don't want these to be matched with the strings.
The thing is, brackets are special identifiers in string patterns that allow you to set things such as complements and sets. For this reason, we need to tell the compiler that we aren't closing the complement, but rather looking for anything that isn't [ or ]. To do this, we just put a percent symbol % preceding the brackets.
#, (Comma)
Pretty simple, we don't want to match commas either.
#%s
This is basically anything that is whitespace such as spaces and tabs. We want to ignore these so we add this special identifier to the complement
#+ (Plus)
This is to match 1 or more of the given pattern.
Our pattern basically says, "Match anything that isn't [ or ] or , or whitespace.
The reason we need the plus at the end is because if we left it out, only a single letter would be matched each time. So we want the longest possible sequence we can obtain.
#Putting This Into What You Want
Using this we can easily add support for making StringValues
str = "[0,1,40,40,348,348,348,348,348,348]"
for match in string.gmatch(str, "[^%[%],%s]+" ) do
local strV = Instance.new("StringValue", workspace["somePlace"])
strV.Value = match;
strV.Name = "String Match!"
end
Useful Link: http://wiki.roblox.com/index.php?title=String_pattern
1 comment
Thanks! This worked like a charm :)