user2770820
user2770820

Reputation: 3

How to split string using a whole word as separator?

Is there a way to split string like:

"importanttext1 has gathered importanttext2 from stackoverflow."

I want to grab anything before the word "has", and just want to grab the one word after "gathered", so it doesn't include "from stackoverflow.". I want to be left with 2 variables that cointain importanttext1 and importanttext2

Upvotes: 0

Views: 272

Answers (1)

Yu Hao
Yu Hao

Reputation: 122493

local str = "importanttext1 has gathered importanttext2 from stackoverflow."
local s1 = str:match("(.+)has")
local s2 = str:match("gathered%s+(%S+)")
print(s1)
print(s2)

Note that "%s" matches a whitespace character while "%S" matches a non-whitespace character.

Upvotes: 1

Related Questions