문제

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