Question

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

Was it helpful?

Solution

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top