51 posts
Posted 11 July 2013 - 09:50 AM
Hello everyone,
I need some help with strings and patterns.
To be exact patterns for gmatch. I know that I become with %w+ a whole word and numbers.
str = 'My teststring with numbers 1234 and some symbols!!! '
for word in string.gmatch(str, '%w+') do
print(word)
end
My problem is, I don't get the symbols, but I need them.
Can anyone help me to understand how I use patterns exactly?
Thanks.
~ BlankWolf ~
375 posts
Location
USA
Posted 11 July 2013 - 02:02 PM
It looks like you want to match for things delimited by spaces. You can use the pattern
%S+, which matches strings of characters which are not spaces. Note that case matters, and using lowercase 's' will only match spaces.
This is a good resource for the pattern-matching functions in Lua:
http://www.lua.org/pil/20.1.html
51 posts
Posted 15 July 2013 - 02:30 AM
It looks like you want to match for things delimited by spaces. You can use the pattern
%S+, which matches strings of characters which are not spaces. Note that case matters, and using lowercase 's' will only match spaces.
This is a good resource for the pattern-matching functions in Lua:
http://www.lua.org/pil/20.1.html
Thanks that works fine.
But… I don't understand patterns at all. :(/>
Well. Maybe some time in the future.
375 posts
Location
USA
Posted 15 July 2013 - 06:22 PM
%s+ is a pattern which matches for any number of space characters. The opposite pattern is %S+. Using this with the gmatch loop you were using gives you all characters of characters delimited by spaces.
51 posts
Posted 16 July 2013 - 01:49 AM
%s+ is a pattern which matches for any number of space characters. The opposite pattern is %S+. Using this with the gmatch loop you were using gives you all characters of characters delimited by spaces.
Yes I know that. You say that in your post above.
But %w+, %s+ and so on are not the only part of patterns.
Anyway. It works now.