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

Help with string manipulation?

Asked by 6 years ago

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

0
Use string.sub. hiimgoodpack 2009 — 6y

1 answer

Log in to vote
2
Answered by
XAXA 1569 Moderation Voter
6 years ago
Edited 6 years ago
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.

0
You really should be explaining how this pattern works. User#5423 17 — 6y
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. XAXA 1569 — 6y
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 chess123mate 5873 — 6y
Ad

Answer this question