Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

Get all strings in a much larger string (find the start pos-end pos of the string)?

Basically the title says it all:

-->> My script
print("this is my string! 'this is a smaller string!' my string is back again!")

I want that 'this is a smaller string...' part to be returned in a function e.g.

function getString(text)
return -->> 'this is a smaller string...'
end

Edit: Allow me to explain more, this is for a Script Building game, and I need this to filter any strings in the players' code so they can't swear. I've tried filtering the whole thing, but then it just turns into hashtags. Code:

filterAllTextInstances = function(str)
	for match in string.gmatch(str , '[^\.]+"') do
		print("found: " .. match)
		--str:gsub(match,game:service("ReplicatedStorage").FilterText:InvokeServer(match))
		print("filtered: " .. game.Chat:FilterStringForBroadcast(match,game.Players.LocalPlayer))
		str:gsub(match,game.Chat:FilterStringForBroadcast(match,game.Players.LocalPlayer))
	end
	return str
end

1 answer

In order to do what you're asking the match() function must be used and is part of the string library in Lua. The match() function has a parameter for the string and a parameter for a string pattern. In this case, our pattern must represent any number of characters followed by three dots. This pattern would look something like: [^\.]+%.%.%.. This pattern is basically saying any number of characters that is not a period followed by three periods. The first part inside the square brackets is a character set and consists of any character that is not a period. The plus sign is so that the pattern extends as far as possible. The %.%.%. part of the pattern represents the dots that must follow after the characters. Because dots are special characters, they must be escaped by adding a % sign before. For the string you gave, you could implement it like this:

local string = "this is my string... 'this is a smaller string...' my string is back again..."
local firstString = string.match(s , "[^\.]+%.%.%.")
print(firstString) --> this is my string...

If you want to find every instance of the pattern within a string, you could even use the gmatch() function which is basically an iterator function.

for match in string.gmatch(s , "[^\.]+%.%.%.") do
	print(match)
end

1 comment

0

doesn't work please check edits

This discussion is preserved as it was. New answers, comments, and votes are closed.