如何使用整个单词作为分隔符拆分字符串?

时间:2014-06-25 13:00:40

标签: string lua lua-patterns

有没有办法分割字符串,如:

"importanttext1 has gathered importanttext2 from stackoverflow."

我想在单词"has"之前抓取任何内容,只想在"gathered"之后抓取一个单词,因此它不包含"from stackoverflow."。我希望留下2个汇总importanttext1importanttext2

的变量

1 个答案:

答案 0 :(得分:1)

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

请注意,"%s"匹配空白字符,而"%S"匹配非空白字符。