Scite Lua - escaping right bracket in regex?

Posted by ~sd-imi on Stack Overflow See other posts from Stack Overflow or by ~sd-imi
Published on 2010-03-25T19:43:32Z Indexed on 2010/03/25 19:53 UTC
Read the original article Hit count: 377

Filed under:
|
|
|

Hi all,

Bumped into a somewhat weird problem... I want to turn the string:

a\left(b_{d}\right)

into

a \left( b_{d} \right)

in Scite using a Lua script.

So, I made the following Lua script for Scite:


function SpaceTexEquations()
  editor:BeginUndoAction()
    local sel = editor:GetSelText()

    local cln3 = string.gsub(sel, "\\left(", " \\left( ")
    local cln4 = string.gsub(cln3, "\\right)", " \\right) ")

    editor:ReplaceSel(cln4)
  editor:EndUndoAction()
end

The cln3 line works fine, however, cln4 crashes with:

 /home/user/sciteLuaFunctions.lua:49: invalid pattern capture
 >Lua: error occurred while processing command

I think this is because bracket characters () are reserved characters in Lua; but then, how come the cln3 line works without escaping? By the way I also tried:

-- using backslash \ as escape char:
local cln4 = string.gsub(cln3, "\\right\)", " \\right) ") -- crashes all the same

-- using percentage sign % as escape chare
local cln4 = string.gsub(cln3, "\\right%)", " \\right) ") -- does not crash, but does not match either

Could anyone tell me what would be the correct way to do this?

Thanks,

Cheers!

© Stack Overflow or respective owner

Related posts about scite

Related posts about lua