String pattern not matching entire string

  1. What do you want to achieve? Keep it simple and clear!
    I am trying to doing something where it checks if a string has 2 uppercase characters for a system I’m making.

  2. What is the issue? Include screenshots / videos if possible!
    I can’t seem to match the 2nd uppercase character that is elsewhere in the string.

  3. What solutions have you tried so far? Did you look for solutions on the Developer Hub?
    I’ve tried to match the string with 2 different lines of code, but I still only got the 1st uppercase character and not the second. I’ve looked at the string patterns article and dev forum for a while now and I can’t get it to work.

My code is along the lines of this:

local String = string.match("DestinationName", "%u+") -- Doesn't work. Only returns "D".

if string.len(String) == 2 then
-- Do something
end

I think you can use string.gsub for this. We can use %U, which is for everything not uppercase, use that as the pattern to match and set every match to "" so we only get the uppercase letters

local String = string.gsub("DestinationName", "%U", "")

if string.len(String) == 2 then
-- Do something
end
1 Like

string.match only iterates until the first break after the letters that match the pattern. If you wanted all the upper case letters in a string you would have to use gmatch and a for loop.

for letter in string.gmatch("HeLlO WoRlD!", "%u+") do
	print(letter)
end