String Lua中带空格的模式
如何使用String Lua中带空格的模式,string,lua,lua-patterns,String,Lua,Lua Patterns,如何使用string.gmatch(文本、模式)执行此操作: text = "Hello.%23 Awesome7^.." pattern = --what to put here? for word in string.gmatch(text, pattern) do print(word) end --Result >test Hello.%23 Awesome7^.. > 我一直在使用“%w+%p”,但这会导致: >test Hello. % 23 Awesome7
string.gmatch(文本、模式)
执行此操作:
text = "Hello.%23 Awesome7^.."
pattern = --what to put here?
for word in string.gmatch(text, pattern) do
print(word)
end
--Result
>test
Hello.%23
Awesome7^..
>
我一直在使用“%w+%p”
,但这会导致:
>test
Hello.
%
23
Awesome7^
.
.
这不是期望的结果
注意:我没有测试这个精确的字符串,它可能会有所不同。。。但是,仍然不能创建所需的结果。根据您的示例,每个单词都不包含空格,并且由空格分隔,因此最简单的模式是
“%S+”
:
“%s”
匹配空格字符,“%s”
匹配非空格字符。这不会导致gmatch打印“”?编辑:nvm,我不知道大写字母有什么不同things@KingofGamesYami测试一下,注意它是大写的“%s+”
。
text = "Hello.%23 Awesome7^.."
pattern = "%S+"
for word in string.gmatch(text, pattern) do
print(word)
end