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
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
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.
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