質問

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