lua-users home
lua-l archive

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


On Aug 21, 2014, at 5:57 PM, Dirk Laurie <dirk.laurie@gmail.com> wrote:

> That "lengthy" is a challenge ... 

Just for fun, a convoluted global find, gfind, just because… :P

local function gfind( aString, aPattern, isPlain )
  assert( type( aString ) == 'string' )
  assert( type( aPattern ) == 'string' )
  if isPlain then aPattern = aPattern:gsub( '%p', '%%%1' ) end
  aPattern = ( '()(%s)()' ):format( aPattern )

  return coroutine.wrap( function() for aStart, _, anEnd in aString:gmatch( aPattern ) do coroutine.yield( aStart, anEnd ) end end )
end


for aStart, anEnd in gfind( 'aa aa aa', 'aa' ) do
  print( aStart, anEnd )
end

1	3
4	6
7	9