I understand that you can find the complement of a character set. For example, the pattern [^abc]+
will find all characters that are neither "a" nor "b" nor "c".
I'm wondering if or how it's possible to find the complement of a sequence, such that unlike above, I could match all characters that are not the literal sequence "abc" instead of matching everything that's not the character "a" or "b" or "c".
If this is still confusing, this is essentially what I'm trying to do and what kind of result I expect:
local text = "Hello, world! Wonderful day, isn't it?" for a in string.gmatch(text, "Complement of 'day'") do print(a) end -- Output -> -- Hello, world! Wonderful -- Isn't it?
I hope this makes my question clear. Please let me know if you need more clarification.
To simply remove it from the string:
s="Hello abc, howabc are you?" --Remove 'abc': print(s:gsub("(.-)(abc)", "%1"))
Roblox has added string.split to Lua [thanks to GoldAngelInDisguise for mentioning this], meaning you can also do:
print(unpack(s:split("abc"))) -- or local list = s:split("abc") for i = 1, #list do print(list[i]) end
In normal Lua you can do what's shown here:
https://stackoverflow.com/a/1647577/3377142
(Essentially, you can use string.find
to get the index of the next instance abc
(or whatever pattern you choose) and then return the strings in between that pattern.)