lua-users home
lua-l archive

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


On 21 July 2011 20:05, Dirk Laurie <dpl@sun.ac.za> wrote:
> In a certain string of length about 32000, a substring of length about
> 900, running over several lines, occurs thirty-odd times.  Wishing to
> count the actual number of times, I cut-and-pasted it from another
> window into my Lua session:
>
>    item = [[paste here]]
>
> Then I tried:
>
>    _,count = str:gsub(item,'')
>
> and got `count==0`.  Not expected.  Thinking there may have been
> invisible characters, I tried this instead:
>
>    item = str:sub(i,j)
>
> having carefully found what i and j are.  Same result.
>
> I soon discovered that the reason is that `item` contains several
> magic characters.
>
> Quick workaround: devise a pattern that matches `item` and no other
> substring of `str`.  But nicer would be a pattern item that says:
> "treat the rest of this pattern as literal".  Say `%r`, for "raw".
>
> Dirk
>
>

You can also use string.find with the 'plain' option.