Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Help with string manipulation?

I'm pretty sure this involves string patterns, but I'm finding a way to get a group of characters in between two defined strings. For example,

string = "apple orange banana watermelon"

The two defined strings could be "apple" and "banana", so the text in between is " orange ", and that's what it would print.

Is there something like that I can do? Thanks.

-SkeletalReality

1 comment

0

Use string.sub.

1 answer

✓ Accepted answer
local s = "apple orange banana watermelon"
print(string.match(s, "apple(.*)banana")) -- prints " orange "

The pattern string, "apple(.*)banana" captures any string (.*) between apple and banana. string.match will then return this capture.

3 comments

0

You really should be explaining how this pattern works.

0

Did I not explain it enough? Should I make it explicit that (.*) captures zero or more of any character? There really isn't a a whole lot of stuff going on in this relatively simple regex.

0

Tutorial for those who don't know about lua search patterns: http://lua-users.org/wiki/PatternsTutorial Manual: https://www.lua.org/pil/20.2.html

This discussion is preserved as it was. New answers, comments, and votes are closed.