lua-users home
lua-l archive

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




On 20/08/15 11:07 AM, Matthew Wild wrote:
On 19 August 2015 at 21:18, Soni L. <fakedme@gmail.com> wrote:
Are there any use-cases for string.gsub(str, "().", "%1")?

If not, can it please become an error? It's just... weird... unexpected...

(I'm talking about the very weird number to string coercion which happens
there)
I wanted to triumphantly say there was a use-case, in converting text
to netstrings. However unfortunately the result is always off-by-one:

   ("hello world!"):gsub("^(.*)()$", "%2:%1,") -- 13:hello world!,

See https://en.wikipedia.org/wiki/Netstring

However in any case I don't think the fact that position captures can
be used directly as replacements is really an issue, it's pretty much
exactly what I would expect to happen.

Regards,
Matthew

This is Lua.

("hello world!"):gsub("^(.-)()(.?)$", "%2:%1%3,") -- 13:hello world!,

(note: doesn't work for empty string)

--
Disclaimer: these emails are public and can be accessed from <TODO: get a non-DHCP IP and put it here>. If you do not agree with this, DO NOT REPLY.