View Single Post
06/04/14, 08:36 PM   #24
Garkin
 
Garkin's Avatar
AddOn Author - Click to view addons
Join Date: Mar 2014
Posts: 832
Originally Posted by Sharlikran View Post
Q1) Did I do it right?
Q2) Why does 3 work in all cases. I tried a 2 for the first example with only v1, and v2 and I got a number and a string.
If it works, you did it right
Lua Code:
  1. select(3, string.find(name, pattern))
  2. -- or if you use colon syntax
  3. select(3, (name):find(pattern))
This works because string.find(...) returns starting and ending position of the pattern in the string and then captures that you have enclosed in brackets. If pattern is not found, return value is nil.

Lua Code:
  1. texture = art/maps/glenumbra/daggerfall_base_0.dds
  2. pattern = "([%w%-]+)/([%w%-]+_[%w%-]+)"
  3.  
  4. string.find(texture, pattern)
  5. --or colon syntax:
  6. (texture):find(pattern)
  7. --returns: 5, 34, "glenumbra", "daggerfall_base"
Pattern "(maps/)([%w%-]+)/([%w%-]+_[%w%-]+)" has one more closure, so thats why I have used select(4,...).

Last edited by Garkin : 06/04/14 at 09:00 PM.
  Reply With Quote