lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


On 10/30/2013 10:59 AM, Luiz Henrique de Figueiredo wrote:
> How about this?
> 
> s='the quick brown fox jumps over the lazy dog'
> w='the'
> 
> print(s:find(w))		-- first match (position is first return value)
> print(s:find('.*()'..w))	-- last match (position is third return value)

Just an implementation detail, but I think it would be advisable to tie it
to the start of the string, making the case of failed matches much quicker;
I believe it produces the same results:
print(s:find('^.*()'..w))

> s='the quick brown fox jumps over the lazy dog'
> w='the'
> sstr='.*()'..w
> c1=os.clock() for i=1,999999 do s:find(sstr) end print(os.clock()-c1)
0.68
> w='foo'
> sstr='.*()'..w
> c1=os.clock() for i=1,999999 do s:find(sstr) end print(os.clock()-c1)
26.53
> sstr='^.*()'..w
> c1=os.clock() for i=1,999999 do s:find(sstr) end print(os.clock()-c1)
1.33