Is there a way to ignore a string pattern?

I’m trying to do something that’s essentially the opposite of string.match, where I only return the part of the string that doesn’t match the given string pattern.

I have two tables for magic, one refers to buttons, the other just stores data.

local ParticleData = {
	['Fire'] = false;
	['Shadow'] = false;
	['Lightning'] = false;
	['Light'] = false;
	['Divinity'] = false;
}

local ParticleEffectConsoles = {
	['FireParticle'] = script.Parent.AddFire;
	['ShadowParticle'] = script.Parent.AddShadow;
	['LightningParticle'] = script.Parent.AddLightning;
	['LightParticle'] = script.Parent.AddLight;
	['DivinityParticle'] = script.Parent.AddDivinity;
}

I’m trying to iterate through ParticleEffectConsoles and match ‘Fire’, ‘Shadow’, ‘Lightning’, etc with the table above it. How would I go about filtering out the ‘Particle’ bit of the string so it only returns the needed information?

1 Like

You can call string.split which returns a tables of the characters separated by the pattern. In this case, you’d call it with the separator as “Particle” and index the first set of characters like so:

local function ParseParticleName(str)
	if str:find("Particle") then
		return str:split(str, "Particle")[1]
	end
end

for i,v in pairs(ParticleEffectConsoles) do
	print(ParseParticleName(i))
end

You can learn more about string manipulation here. Hope this helps! :slight_smile:

3 Likes

Using the string pattern "(.*)Particle" in string.match will return all characters leading up to the word Particle.

local ParticleData = {
	['Fire'] = false;
	['Shadow'] = false;
	['Lightning'] = false;
	['Light'] = false;
	['Divinity'] = false;
}

local ParticleEffectConsoles = {
	['FireParticle'] = script.Parent.AddFire;
	['ShadowParticle'] = script.Parent.AddShadow;
	['LightningParticle'] = script.Parent.AddLightning;
	['LightParticle'] = script.Parent.AddLight;
	['DivinityParticle'] = script.Parent.AddDivinity;
}

for name, console in pairs(ParticleEffectConsoles) do
	ParticleType = string.match(name, "(.*)Particle")
	print(ParticleType..": "..tostring(ParticleData[ParticleType]))
end

The parenthesis are used to capture the pattern enclosed, the period is used to match any character and the asterisks is used to match as many of those characters that lead up to the word “Particle”.

Hope this helps!

5 Likes

What you can do is use string.gsub to delete a pattern in a string, like str:gsub("Particle", "")

2 Likes