lua-users home
lua-l archive

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


Am 22.08.2014 um 00:17 schröbte Petite Abeille:

On Aug 22, 2014, at 12:02 AM, Sean Conner <sean@conman.org> wrote:

  Yes, it has a dependency on LPeg.

Yes, it does indeed :P

function escape( s ) return ( s:gsub( '%p', '%%%1' ) ) end

I'm a bit puzzled why this one works. `%1` is supposed to refer to the first capture, but there is no capture in your pattern ...

function replace( s, this, that ) return ( s:gsub( escape( this ), escape( that ) ) ) end

Also on Lua 5.2+ you need a different escape function for the replacement string

    function escape2( s ) return ( s:gsub( '%%', '%%%%' ) ) end

or else

    print( replace( '10 plus 10 = 20', 'plus', '+' ) )

    /usr/bin/lua: ./repl.lua:5: invalid use of '%' in replacement string


Philipp