String matching

You can write your topic however you want, but you need to answer these questions:

  1. What do you want to achieve? Keep it simple and clear!
    i want the below to not return nil
    string.match(“Text”,“The Text”)

  2. What is the issue? Include screenshots / videos if possible!
    the issue is that i am not very experienced in string manipulation
    but it seems like i can not avoid it this time in the script im writing

  3. What solutions have you tried so far? Did you look for solutions on the Developer Hub?
    i looked on google but the solution gave was something similar to this

string.match(“i have a bag”, “bag”) = bag

what i want is this but that returns nil
string.match(“i have a bag”, “the bag”)

1 Like

what exactly are you using this for if i may ask
also, it will return nil because ‘i have a bag’ does contain the matched words ‘the bag’

1 Like

It seems as if you’re not understanding string.match properly.

Here’s a link to the DevHub’s page on strings: string

Additionally, here’s a link to the Luau library documentation: Library - Luau

this was the script im writing

local groupService = game:GetService'GroupService'
local UserId = 1556794251

local groups = groupService:GetGroupsAsync(UserId)
local groupsInfo = {}

for _, groupInfo in pairs(groups) do
	table.insert(groupsInfo, (groupInfo.Rank == 255) and groupInfo.Name or nil)
	table.insert(groupsInfo, (groupInfo.Rank == 255) and groupInfo.Id or nil)
end

local replicatedStorage = game:GetService'ReplicatedStorage'
local remoteEvent = replicatedStorage.RemoteEvent

remoteEvent.OnServerEvent:Connect(function(player, text)
	
	for i, v in pairs(groupsInfo) do
		
		local match = tostring(v):match(text)
		print(match)--this is the part that went wrong where it didnt match anything
                --because the computer is trying to match "The Royal Family" to get string "Royal Family"
                --and it didnt find anything because the computer is thinking the pattern as a whole
                --where as i only am trying to use a part of it not the entire thing
                --but i dont know how to delete "The" without effecting the text on the player's screen
		if match ~= nil then
			local groupId = groupsInfo[i + 1]
			print(groupId)
			local rank = player:GetRankInGroup(groupId)
			
			print(rank)
			
			
		end
		
	end
	
end)

why not use string.match to match (“The”) and find the start and end index and then substring to remove the ‘The’

It returns nil because the subject string doesn’t contain the string pattern (search string).

If you want to remove “The” from a string just do the following.

local string1 = string.gsub("i have the bag", "the", "a")
print(string1) --i have a bag

string.find() would be the better alternative as it returns the start & end index of the first search match.