It's possible I'm misunderstanding what the goal is here, but why aren't you just matching the word pattern you're searching for?
eg.
Lua Code:
for word in string.gmatch('function someFunction(t,f) print(t,f) end', '[_%a][_%w]+') do print(word) end
Code:
function
someFunction
print
end