lua gsub特殊替换产生无效的捕获索引

发布于 2024-12-10 05:48:48 字数 467 浏览 0 评论 0原文

我有一段lua代码(在Corona中执行):

local loginstr = "emailAddress={email} password={password}"
print(loginstr:gsub( "{email}", "[email protected]" ))

此代码生成错误:

无效的捕获索引

虽然我现在知道这是因为在gsub模式中没有正确指定大括号,但我不知道如何修复它。

我应该如何形成 gsub 模式,以便可以用电子邮件地址值替换占位符字符串?

我浏览了所有我能找到的面向 lua 的网站,但大多数文档似乎都是围绕不相关的情况展开的。

I have a piece of lua code (executing in Corona):

local loginstr = "emailAddress={email} password={password}"
print(loginstr:gsub( "{email}", "[email protected]" ))

This code generates the error:

invalid capture index

While I now know it is because of the curly braces not being specified appropriately in the gsub pattern, I don't know how to fix it.

How should I form the gsub pattern so that I can replace the placeholder string with the email address value?

I've looked around on all the lua-oriented sites I can find but most of the documentation seems to revolve around unassociated situations.

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

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

发布评论

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

评论(3

半步萧音过轻尘 2024-12-17 05:48:48

正如我在上面的评论中所建议的,当电子邮件被编码为 URL 参数时,用于编码“@”字符的 %40 将用作捕获索引。由于搜索模式没有任何捕获(更不用说 40 个捕获),这将导致问题。

有两种可能的解决方案:您可以对编码的字符串进行解码,或者对替换字符串进行编码以转义其中的“%”字符。根据您要对最终结果执行的操作,您可能需要同时执行这两项操作。

以下例程(我从此处获取 - 未测试)可以解码编码字符串:

function url_decode(str)
  str = string.gsub (str, "+", " ")
  str = string.gsub (str, "%%(%x%x)",
      function(h) return string.char(tonumber(h,16)) end)
  str = string.gsub (str, "\r\n", "\n")
  return str
end

用于转义字符串中的 % 字符str,您可以使用:

str:gsub("%%", "%%%%")

'%' 字符被转义为 '%%',并且需要在搜索模式和替换模式上进行转义(因此 % 字符的数量代替)。

As I've suggested in the comments above, when the e-mail is encoded as a URL parameter, the %40 used to encode the '@' character will be used as a capture index. Since the search pattern doesn't have any captures (let alone 40 of them), this will cause a problem.

There are two possible solutions: you can either decode the encoded string, or encode your replacement string to escape the '%' character in it. Depending on what you are going to do with the end result, you may need to do both.

the following routine (I picked up from here - not tested) can decode an encoded string:

function url_decode(str)
  str = string.gsub (str, "+", " ")
  str = string.gsub (str, "%%(%x%x)",
      function(h) return string.char(tonumber(h,16)) end)
  str = string.gsub (str, "\r\n", "\n")
  return str
end

For escaping the % character in string str, you can use:

str:gsub("%%", "%%%%")

The '%' character is escaped as '%%', and it needs to be ascaped on both the search pattern and the replace pattern (hence the amount of % characters in the replace).

日暮斜阳 2024-12-17 05:48:48

您确定问题不是您尝试在 loginurl 上使用 gsub 而不是 loginstr 吗?

您的代码给了我这个错误(请参阅 http://ideone.com/wwiZk):

lua: prog.lua:2: attempt to index global 'loginurl' (a nil value)

这听起来类似于你看到了。只需修复它以使用正确的变量:

print(loginstr:gsub( "{email}", "[email protected]" ))

说(参见 http://ideone.com/mMj0N):

[email protected] password={password}

根据需要。

Are you sure your problem isn't that you're trying to gsub on loginurl rather than loginstr?

Your code gives me this error (see http://ideone.com/wwiZk):

lua: prog.lua:2: attempt to index global 'loginurl' (a nil value)

and that sounds similar to what you're seeing. Just fixing it to use the right variable:

print(loginstr:gsub( "{email}", "[email protected]" ))

says (see http://ideone.com/mMj0N):

[email protected] password={password}

as desired.

⊕婉儿 2024-12-17 05:48:48

我在 value 部分中有这个,所以你需要使用:value:gsub("%%", "%%%%") 来转义值。

替换 json 中“某些值”的示例:

local resultJSON = json:gsub(, "\"SOME_VALUE\"", value:gsub("%%", "%%%%"))

I had this in value part so You need to escape value with: value:gsub("%%", "%%%%").

Example of replacing "some value" in json:

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