Help with string.gsub()!

Helloo!! I’ve been scripting for a long time now but there’s something I’ll never understand. string patterns :broken_heart:

I’m trying to replace a part of a string using string.gsub() if they have these properties:

  • They are the start of the string — no spaces or any characters before.
  • Starts with <prefix and ends with > (just the part, not the whole strings end)
  • The prefix part can be followed by spaces or other strings separated by spaces
    • In this case, the strings should be separated by a space from prefix (Ex.: “<prefixlol>” shouldnt count)

Overall, the accepted strings should look like these;

  • “<prefix>” & “<prefix> bla bla bla”
  • “<prefix >” (space between prefix and ‘>’)
  • “<prefix but not>”
  • “<prefix again >”

Thanks in advance!! :​D

:grey_exclamation: It seems like the < and > char hides text on the forum, so I recommend putting a \ before them lols

I don’t think you can do that with just a gsub() you’d have to first check if the string has what you want using other string commands such as match and sub and only then doing gsub

Can you provide examples and your desired results for those examples? The current examples seem to be cut off or unclear.

But, if you want to match <someStringHere> only at the start, something like this could work:

local example = "<someStringHere>"
local replacedString = string.gsub(example, "^%b<>", "hello")
print(replacedString) --> "hello"
  • ^ matches the start of the string.
  • %b is the flag for symmetric matching, so %b<> matches the first <, then keeps on searching until it finds an equivalent quantity of >.

By the way, if you don’t want the formatting to hide things, you can use backticks <like this> and it will display properly:

image

1 Like

Of course!
Basically <mark ...> and <mark> should be replaced. (mark ... represents the cases where mark is followed a space and then any string)

"<mark> lol"  -> "hello lol"
--mark.

"<mark > againnn"  ->  "hello againnn"
--mark with a space before ">"

"<mark but more words> world"  ->  "hello world"
--mark followed by some words

"<mark example > :P" -> "hello :P"
--mark followed by words and space before the ">"

"<mark but_its_weird> :P" -> "hello :P"
--mark followed by word(s) but in another format

"<markbutevil> example" -> "<markbutevil> example"
"<marklol thing> example" -> "<marklol thing> example"
--mark followed by word(s) WITHOUT a space first
--this case shouldnt be replaced/qualified

If you need more examples then tell me

So if I’m understanding correctly, you want to match strings that start with <mark and end with >, but if any extra words are in between, there has to be a space.

This should work:

local example = "<mark but_its_weird> :P"
local replacement = "hello"
local result = string.gsub(example, "^%b<>", function(capture)
	print(capture) --> "<mark but_its_weird>"
	
	local sixthChar = string.sub(capture, 6, 6)
	if sixthChar == " " or sixthChar == ">" then
		return replacement
	else
		return capture
	end
end)
print(result) --> "hello :P"

We are checking if the 6th character is a space or the ending “>” to see if it’s valid or not. If it is, sub in the replacement. If it isn’t, return the original capture.

1 Like
local function replace_prefixes(text, prefix, replacement)
  local pattern = "^\\<" .. prefix .. "%s+>" -- or "^\\<" .. prefix .. "[^\\>]+\\>" Im dont sure
  local new_text = string.gsub(text, pattern, replacement)
  return new_text
end

Thanks!! I modified the code a bit and got it working :​)

--function for string.gsub
local mark = 'mark'
local check = string.sub(str,1,#mark+2)
if check == '<'..mark..'>' or check == '<'..mark..' ' then
	return 'replacement'
else
	return str
end