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

Re: Searching for >> not >>>



In message <58421c87b2list01@xxxxxxxxxxxxxxxx>
          Martin <list01@xxxxxxxxxxxxxxxx> wrote:

>I am sure this should be simple ... but how do I search for instances
>of ">>" but I do not want ">>>" to be included?

Not so simple, I suspect. Here is a script using lpeg. Note
that the pattern is defined recursively as a grammar. I do not think
it is possible to do this without (prioritized) alternatives,
which rules out Lua's standard pattern-matching.

#!lua
local lnum = 0
local match, P, V in lpeg
local arr = P ">"
local arr2 = arr*arr
local other = 1 - arr
local start = arr2*other  -- beginning of line
local stop = arr2*P (-1)  -- end of line
local p = start + stop
local pat = P { p + other * V(1) } -- grammar
for line in io.lines (arg[1]) do
    lnum += 1
    if match (pat, line) then
      print (lnum, ":", line)
    end -- if
end -- for

--
Gavin Wraith (gavin@xxxxxxxxxxxxxxx)
Home page: http://www.wra1th.plus.com/