Regex, splitting a gmatch into letters?

I’m attempting to make it so every argument inside message is returned as a letter instead of a word and then added to the table as a letter.

I know there’s topics for this but I don’t understand how I would make it work with the existing regex, I have tested it alone and it works though to my understanding % is used if there are other regex-characters in the string please correct me on this if I’m wrong, it’s important that I understand % better.
I’ve been using this script to help better my understanding of Regex.

At the moment if I add the %. it breaks the script and it’ll no longer run.

local function ParseCommand(player, message)
	local message = textVal.Text
	local Arguments = {} 
	for Argument in message:gmatch("%.[^%s]+") do
		table.insert(Arguments, Argument)
		print(Arguments)		
	end

The % symbol in Lua string patterns is used as an escape character. So you would use it if you want to directly match a character that is usually used within the pattern matching system. For instance, the . character is used to match any character. However, what if you want to actually match a period .? In that case, you would escape the symbol using %. to indicate you want to match the period and not just any symbol.

1 Like

Then why isn’t it working here? I put %.[^%s]+ and it will instead just break the script and prevent it from running. That’s if I am to gather from what you’re saying is that %. would tell lua that it was to search for any character.

This may be happening because it’s performing operations on the first argument was my first thought so I quickly changed the command that performs this operation to t in order to make sure that the loop wasn’t breaking up the first argument(hacky, but this is merely a test to further my knowledge.)

Flip it around. %. will look for periods, while just . will look for any character

1 Like

Understood, thank you! I do have another query if you don’t mind, though it’s not related so it’s fine if you ignore it.
Got it working now, though your reply wasn’t exactly a solution it was very helpful in better understanding regex so I appreciate it!

Forgot to ask, [] encompassing it means it’ll search for anything specified inside the brackets. I.E
“[^.%s]” means it’ll search for a letter and whitespace, if it finds either of them it’ll move on to the next one. Allowing me to add a letter to a table and ignore white space at the same time.

Am I accurate in this?