Frage

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

War es hilfreich?

Lösung

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.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top