Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
3

String Patterns - Finding the Complement of a Character Sequence?

Asked by 4 years ago
Edited 4 years ago

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.

1
i think string.split may be what you want https://developer.roblox.com/en-us/api-reference/lua-docs/string/ you may be aware of it but im leaving this here just in case GoldAngelInDisguise 297 — 4y
0
You kind of start to not make sense here: "... 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"." Can you clarify what you meant?. User#24403 69 — 4y
0
The closest solution for your preferred method is by using Frontier pattern matching. I will post an example after I come back home. Zafirua 1348 — 4y
0
@incap I'm not really sure how else to phrase it. I'm essentially just trying to match all characters that are not in a given sequence. For example, if I wanted to ignore "Wo" in "Hello, World!", the expected result should be "Hello, " \n "rld!" ScriptGuider 5640 — 4y

1 answer

Log in to vote
1
Answered by 4 years ago
Edited 4 years ago

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

0
split seems more useful, it was more a matter of parsing a string by using a sequence of characters as a delimiter, instead of just removing the sequence. Either way, super useful answer, thank you. ScriptGuider 5640 — 4y
0
the annoying part seemed to be finding a method that included the delimiters in the parsing. I think the best route at this time is just to loop through all the characters. ScriptGuider 5640 — 4y
0
If the delimiter is a few characters (but not a pattern), you can use `split` and know that for every loop iteration except the last, the delimiter is present after the string given by split. If it's a pattern, you can use repeated calls to 'find' to find the next instance of the delimiter pattern and use `sub` to extract the characters in between (instead of looking at one char at a time) chess123mate 5873 — 4y
0
yeah, the only problem is my delimiter is "[...]" so what's between the two brackets is variable. Essentially, I want to be able to split "hello [world] there" into "hello", "[world]", "there" ScriptGuider 5640 — 4y
View all comments (2 more)
0
I developed the pattern: ".-[^%[%]]+]?" to solve exactly that, but it doesn't account for nested cases like "[[ ]]", so I'm pretty sure I have to stick with looping through the string and keeping track when the delimiter starts/ends ScriptGuider 5640 — 4y
0
Ah, I see - I believe you're right. Of interest, some implementations of regex outside of Lua can handle that with "recursion" or "balanced groups": https://stackoverflow.com/questions/546433/regular-expression-to-match-balanced-parentheses?noredirect=1&lq=1 chess123mate 5873 — 4y
Ad

Answer this question