dail8859 / LuaScript

Notepad++ plugin for Lua scripting capabilities
https://dail8859.github.io/LuaScript/
GNU General Public License v2.0
109 stars 23 forks source link

invalid escape sequence #70

Closed vanmorden closed 5 years ago

vanmorden commented 5 years ago

The following text and code is in the current file:

-- "NUM-0002218307";"equipment1";"";"text with any character -:.áéíóúÁÉÍÓÚ";"STATE";"12/06/2019 00:00:00";"otherData1";"otherData2";"otherData3";"otherData4";"otherData5";"otherData6"
-- "NUM-0002218308";"equipment2";"group";"text with any character -:.áéíóúÁÉÍÓÚ";"STATE";"13/06/2019 00:00:00";"otherData1";"otherData2";"otherData3";"otherData4";"otherData5";"otherData6"

local flags = SCFIND_REGEXP | SCFIND_CXX11REGEX
local regex = ("(?<=\")[A-Za-z0-9\- :\./áéíóúÁÉÍÓÚ]*(?=\";)"):format("\r\n")
for m in editor:match(regex, flags) do
    print("Match pos: " .. m.Pos .. "Match text: " .. m.text)
end

Press run current file results in

File:6: invalid escape sequence near '"(?<=")[A-Za-z0-9\-'

If regex var is set like:

local regex = ("(?<=\")[A-Za-z0-9- :./áéíóúÁÉÍÓÚ]*(?=\";)"):format("\r\n")

The script runs well, but nothing is found.

If use the first regex set in the Notepad++ search window it works properly and finds all quoted text.

dail8859 commented 5 years ago

What version of Notepad++ and LuaScript are you using?

vanmorden commented 5 years ago

LuaScript v0.8 (64bit)

Notepad++ v7.7 (64-bit) Build time : May 19 2019 - 13:05:35 Path : D:\ProgramasInstalados\Notepad++\notepad++.exe Admin mode : OFF Local Conf mode : OFF OS : Windows 10 (64-bit) Plugins : CodeAlignmentNpp.dll ComparePlugin.dll DSpellCheck.dll LuaScript.dll mimeTools.dll NppConverter.dll NppFavorites.dll NppFTP.dll NppSnippets.dll SQLinFormNpp64.dll XMLTools.dll

dail8859 commented 5 years ago

(I modified your original post a bit to add lua syntax highlighting to the code snippets so it is easier to read)

I took this single line from your code:

regex = ("(?<=\")[A-Za-z0-9\- :\./áéíóúÁÉÍÓÚ]*(?=\";)"):format("\r\n")

And if I run this in the LuaScript console, I get:

Console:1: invalid escape sequence near '"(?<=")[A-Za-z0-9\-'

So (luckily) it means that it is something to do purely with Lua and the syntax. The root problem is that \- is not a valid escape sequence. To fix this you either need to add a double backslash (as in \\-) any time you need to handle this type of situation, or Lua also supports literal strings (see same reference) by surrounding the text in double brackets.

For example, both of these should be sufficient for specifying the regex you need:

regex = ([[(?<=\")[A-Za-z0-9- :./áéíóúÁÉÍÓÚ]*(?=\";)]]):format("\r\n")
regex = ("(?<=\")[A-Za-z0-9\\- :\\./áéíóúÁÉÍÓÚ]*(?=\";)"):format("\r\n")

The first line is using the double brackets, the second line adds a double backslash in front of the - and the .

Hope this helped.

vanmorden commented 5 years ago

I'm afraid it doesn't work. If you use your modification, nothing is found by match command. All text in qoutes must be printed with the code of the first post. I want to find a dot (.) or hyphen (-) and you need to put a backslash before them. Whether you put two backslash the pattern change and you search for a backslash followed by any other character in the case of the dot. I think it's a problem with the way you treat the pattern before sending it to Lua.

Anyway, thanks for helping me.

vanmorden commented 5 years ago

Sorry. Your changes work properly, forget my last post. There was a bug in my code. The print command was incorrect. This is the correct. print("Match pos: " .. m.pos .. "Match text: " .. m.text)