String.split question

So how would i split a string, but not only with space also with . and , and :

like this:

local words = string.split(promptTextBox.Text, " " or "," or "." or ":")

Hold on actually, that wouldnt work, i misread it, my apologies

I made a new post if you could help me there that would be nice, I found a solution for this, but not for keep the delimiters.

You can try doing it letter by letter:

local function splitMultiple(s: string, ...: string): {string}
	local parts, args, current = {}, {...}, ""
	for i = 1, s:len() do
		local letter = s:sub(i, i)
		if table.find(args, letter) then
			table.insert(parts, current)
			current = "" 
		else
			current ..= letter 
		end
	end
	if current ~= "" then table.insert(parts, current) end
	return parts 
end

--example usage:
local s = "Hello World!"
print(splitMultiple(s, " ", ",", ".", ":"))
local function normalizeAndJoin(input)
	local normalizedInput = input:gsub("[ ,.:]", " ")
	local words = {}
	for word in string.gmatch(normalizedInput, "%S+") do
		table.insert(words, word)
	end
	return table.concat(words, " ")
end

local promptTextBox = { Text = "Hello, this is an example: removing matches." }
local sentence = normalizeAndJoin(promptTextBox.Text)

print(sentence)

This is a common way to deal with this. Create a pattern set, use it to match vs string while rebuilding.
I remade it to just return the sentance and not split the list…

use regex

local matched = string.gmatch("Hello, World! how are you?", "[^.,;!%s]+")
local splitResult = {}
for word in matched do
	table.insert(splitResult, word)
	-- iterates over each word
end
print(splitResult)
--[[
	{
		Hello
		World
		how
		are
		you?
	}
]]

Instead of returning a table, you will receive an iterator that, when called, returns each matched word in order. Note that you do not need to use pairs or ipairs with the result, as it is already an iterator.

regex explained:

  • the [] are used to make a group of symbols to match
  • the ^ inside the [] is used to say “anything except these symbols”
  • and the + at the end of the [] is saying “match at least one or more” this is to stop it from matching empty words
  • %s means white space

so its matching anything that is not . , ; or a space and not anything empty, resulting in every word