[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: String search: the need for a "literal" pattern
- From: HyperHacker <hyperhacker@...>
- Date: Thu, 21 Jul 2011 13:50:57 -0600
On Thu, Jul 21, 2011 at 04: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
>
>
>
I've wondered before why string.find has a "plain" option but
string.gsub doesn't. It seems wasteful to use the pattern-matching
engine just to replace literal substrings...
--
Sent from my toaster.