Help with string finding

I’m trying to find the word pee in a string but words like speed go through and also flag. print(("LOL SPEED"):lower():find("pee")). What do?

2 Likes

What I meant like i’m using find() to find the word pee in strings, but words like speed also get found due to pee being in the word.

1 Like

ah I see, replace the match with find instead.

1 Like

I did and it worked, curious, what behavior does “^” mitigate?

1 Like

$: Represents the end of a string.

%: Is used as an escape character to treat the following character as a literal character instead of a special pattern character. For example, %a represents any alphabetic character.

^: Is used as an anchor character to match the beginning of a string.

An asterisk: Represents zero or more occurrences of the preceding pattern. For example, a* matches zero or more occurrences of the letter a.

( and ): Are used to enclose a group of patterns. They are primarily used for capturing substrings or creating subpatterns.

a period: Represents any single character except a newline.

[ and ]: Are used to enclose a set of characters. They match any single character within the set. For example, [aeiou] matches any vowel.

+: Represents one or more occurrences of the preceding pattern. For example, a+ matches one or more occurrences of the letter ‘a’.

-: Is used inside a set (enclosed within [ and ]) to represent a range of characters. For example, [0-9] matches any digit.

?: Represents zero or one occurrence of the preceding pattern. For example, a? matches zero or one occurrence of the letter ‘a’.

More special or REGEX like characters for string patterns can be found in the documentation here.

2 Likes

edit: this script should work better, you don’t have to use extra spaces:

local Original = "pee"
local Text = "pee"

for Capture in string.gmatch(Text:lower(),"(%w+)") do
	if string.find(Original,Capture) then
		print(string.find(Text:lower(),Capture))
	end
end
1 Like

How would this work in a function like this?

--words is a table with strings of blacklisted words
function CheckIfHas(Message:string?)
    for _,v in words do
        if Message:lower():find(v) then
            return true
        end
    end
end
function CheckIfHas(Message:string?)
    for _,v in words do
		if Message:lower():find("%W"..v.."%W") then
		  return true
		end
    end
end

if you want only the word “pee” to get flagged, then try this

local str = "pee speed"

local function findthepee(input)
	input = input:lower()
	
	for _, word in pairs(input:split(" ")) do
		if word == "pee" then
			return true -- :))))
		end
	end
	
	return false -- :((((
end

local pee = findthepee(str)

print(pee)

Sadly returns false.

I want to check multiple words not just pee.

That is correct, which is why I return true, because if it is, a certain function is called. (Muting player)
I also tried to use this method, but words like peew go through the system and bypasses it, what do?

function Blacklist(Message:string?)
   for _,v in Message:lower():split(" ") do
		if table.find(words, v) then
			return true
		end
	end
end

if the other script didnt work, you can use this one instead. let me know if this one works:

local wordList = {"pee","badword"}

local function Blacklist(Text,Blacklisted)
	local Text = Text:lower()
	for Capture in string.gmatch(Text,"(%w+)") do
		if table.find(wordList,Capture) then
			return true
		end
	end
	return false
end

print(Blacklist("peeq",wordList))

You seems like you wanna make a system to filter the bad words right? there is already buit-in text filtring feature in roblox if you don’t know.

words like pee or mommy arent banned though

Idk about pee, but for mommy yeah it’s not.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.