开发者

Scite Lua - escaping right bracket in regex?

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开发者_Python百科 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!


The correct escape character in Lua is %, so what you tried should work, I just tried

local sel = [[a\left(b_{d}\right)]]
local cln3 = string.gsub(sel, "\\left%(", " \\left( ")
local cln4 = string.gsub(cln3, "\\right%)", " \\right) ")
print (cln4)

and got

a \left( b_{d} \right) 

so, this worked for me when I tried it, what did you get as a match when you tried %

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜