How could I use string.gmatch(text, pattern) to do this:

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^..
>

I have been using "%w+%p", but this results in:

>test
Hello.
%
23
Awesome7^
.
.

Which is not the desired result.

Note: I have not tested this exact string, it could vary... but still, does not create the desired result

有帮助吗?

解决方案

From your example, every word contains no spaces, and are separated by spaces, so the simplest pattern is "%S+":

text = "Hello.%23 Awesome7^.."
pattern = "%S+"
for word in string.gmatch(text, pattern) do
  print(word)
end

"%s" matches a space character, "%S" matches a non-space character.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top