Lua 中带有单反斜杠 (!) 的字符串需要转义或替换 - 如何操作?

发布于 2025-01-06 14:25:13 字数 462 浏览 0 评论 0原文

Adobe Lightroom 中的插件是用 Lua 编写的。为了自学 Lua,我正在通过 SDK 制作自己的插件。 现在我遇到了一种情况,我从名为“images”的表中获取一个字符串,如下所示:

images[1] = "d:\Windows\Temp\LREXPORT\NK119542.tif"

但是Lua当然不喜欢那些未转义的反斜杠。该表的内容根据用户操作而变化。我正在考虑一种解决方案来获取该字符串并对其进行操作以进行进一步处理。

正确的字符串是:

newimages = "d:/Windows/Temp/LREXPORT/NK119542.tif"
newimages = "d:\\Windows\\Temp\\LREXPORT\\NK119542.tif"

浏览了大量帖子和在线教程,但抱歉,我无法弄清楚这一点。 希望有解决办法,谢谢回复。

In Adobe Lightroom a Plug-in is written in Lua. In an attempt to teach myself Lua I am working myself through the SDK producing own Plug-ins.
Now I came accross a situation where I am getting a string from a table named 'images' like this:

images[1] = "d:\Windows\Temp\LREXPORT\NK119542.tif"

But of course Lua does not like those unescaped backslashes at all. The content of this table varies depending on the user action. I am thinking of a solution to grab that string and manipulate it for further processing.

Correct strings would be:

newimages = "d:/Windows/Temp/LREXPORT/NK119542.tif"
newimages = "d:\\Windows\\Temp\\LREXPORT\\NK119542.tif"

Went through lots of posts and online tutorials but sorry, I was not able to figure this one out.
Hope there is a solution, thanks for replies.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

岁月静好 2025-01-13 14:25:13

您可以使用 [[]] 分隔字符串:

Lua 5.2.3  Copyright (C) 1994-2013 Lua.org, PUC-Rio
> s = '\n'
> print(s)


> s = [[\n]]
> print(s)
\n

You can delimit your string with [[ and ]]:

Lua 5.2.3  Copyright (C) 1994-2013 Lua.org, PUC-Rio
> s = '\n'
> print(s)


> s = [[\n]]
> print(s)
\n
女中豪杰 2025-01-13 14:25:13

如果您可以使用正斜杠,为什么不直接替换它们:

images[i]=images[i]:gsub("\\","/")

如果您认为将它们加倍会对您有帮助,那么同样如此:

images[i]=images[i]:gsub("\\","\\\\")

if your fine with forward slashes, why not just replace them:

images[i]=images[i]:gsub("\\","/")

Same goes if you think doubling them will help you:

images[i]=images[i]:gsub("\\","\\\\")
残疾 2025-01-13 14:25:13

我遇到了同样的问题,对我来说这有效:

user = string.gsub(user, "\\\\", "/")

所以 images[i]=images[i]:gsub("\\\\","/") 也应该有效。

I had the same problem, and for me this worked:

user = string.gsub(user, "\\\\", "/")

so images[i]=images[i]:gsub("\\\\","/") should work as well.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文