Jump to content




[Lua] string.gmatch quick question


3 replies to this topic

#1 W00dyR

  • Members
  • 135 posts

Posted 19 May 2013 - 04:37 PM

Hey guys,

I've been starting to learn more about this function and the stuff it does, but I still don't get a part of it:

For example in this:

for blabla in string.gmatch(text, "%w+") do
	print(blabla)
end

What does the "%w+" exactly mean? In other scripts I've seen things like "[^%s]+" or even "%a+".

I see this differ a lot everywhere, and on the lua manuals I can't really find the origin of the choice of what you type there :/

I do realize that this is a kind of search pattern?

Thanks ahead!

#2 Sammich Lord

    IRC Addict

  • Members
  • 1,212 posts
  • LocationThe Sammich Kingdom

Posted 19 May 2013 - 04:39 PM

This is a pretty good tutorial I found. http://www.wowwiki.c...attern_matching

#3 W00dyR

  • Members
  • 135 posts

Posted 19 May 2013 - 04:44 PM

View PostSammich Lord, on 19 May 2013 - 04:39 PM, said:

This is a pretty good tutorial I found. http://www.wowwiki.c...attern_matching

Ah that is exactly what I was looking for, thanks :)

So if I understand correctly, it's a combination of symbols which all represent a part of the pattern you assign the string.gmatch to look for? So for example if I have a simple line to split, containing both numbers and letters, I start with "%w" so it does both letters and numbers. Then I add "+" to it so it will not stop after its first match? So for what I described "%w+" would work the best?

#4 theoriginalbit

    Semi-Professional ComputerCrafter

  • Moderators
  • 7,332 posts
  • LocationAustralia

Posted 19 May 2013 - 11:39 PM

if you're wanting to do a simple string split then a pattern like so would work fine
self:gmatch('[^'..patt..']+')
where patt is the separator. Using the above pattern in a for loop is the simplest option for splitting a string by a common separator.





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users