:gsub在Lua脚本中表现不如预期

huangapple go评论44阅读模式
英文:

:gsub not working as expected in lua script

问题

Here are the translations of the code parts you provided:

input_string = "smart app"
output_string = "app"
input_string = "smart" or input_string = "smart "
output_string = nil (null)
output_string = input_string:gsub("smart ", "")
input_string = "smart app"
output_string = "app"
input_string = "smart" or imput_string = "smart "
output_string = "smart"
input_string = "smr" or "smr "

I hope this helps with your Lua script! If you have any more questions or need further assistance, feel free to ask.

英文:

I am writing a lua script & want to perform an operation like-

input_string = "smart app"
output_string = "app"
input_string = "smart" or input_string = "smart "
output_string = nil (null)

I am using the below lua snippet to replace "smart " with empty string so that it solves both the cases.

output_string = input_string:gsub("smart ", "")

However I get below responses for each case respectively.

input_string = "smart app"
output_string = "app"
input_string = "smart" or imput_string = "smart "
output_string = "smart"

Why does it not work for case 2? Where am I making mistake? Please suggest.

P.s. I do not want to write separate logic to handle both cases, ideally my snippet should solve the case in one line.

UPDATE QUESTION:

in case

> input_string = "smr" or "smr "

I mean any other word, then also I expect output_string = nil

You could say that all I want is the second word of the string with exactly one space between first & second word. For anything else, I want a null output.

答案1

得分: 1

input_string = "smart" or input_string = "smart ", output_string = nil (null)

这不可能仅使用 gsub 实现。gsub 总是会生成一个字符串。如果你想将空字符串 "" 转换为 nil,你将需要编写额外的逻辑:if output_string == "" then output_string = nil end

input_string = "smart" 被错误地转换为 output_string = "smart"

你的模式包含尾随空格。要进行匹配,你的输入必须包含以空格结尾的 smart。只有 smart 不以空格结尾。

将空格设为可选项:smart ? 会尽力匹配一个空格,但也接受没有空格。注意:这也会将 smartass 转换为 ass

与正则表达式中的 ( |$) 一样,匹配字符串结尾 空格是不可能的,因为不支持模式中的交替 (|)。

或者,你可以通过直接处理 "smart" 的方式使用额外的逻辑:if input_string == "smart" then output_string = "" end。这将正确处理 smartass,保持其不变。

请注意,你可能希望使用 %s 代替文字空格,以接受其他空格字符,例如制表符。你还可能需要使用量词,如 * 来接受多个空格字符。我建议使用 output_string = input_string:gsub("^smart%s*", "")


关于你的更新:

input_string = "smr" or "smr "
>
> 我的意思是任何其他单词,我也希望 output_string = nil
> 你可以说我只想要字符串的第二个单词,第一个单词和第二个单词之间恰好有一个空格。对于其他情况,我希望得到空值输出。

在这种情况下,你绝对想要 output_string = input_string:match("^smart (.+)")

解释:这个模式期望字符串以 smart 开头,然后贪婪地捕获直到结尾的其他字符。如果你希望支持如果字符串只是 "smart " 时为空字符串,使用 .* 替代 .+

如果字符串不以 smart 开头,这个模式不会匹配,结果将为 nil

英文:

> input_string = "smart" or input_string = "smart ", output_string = nil (null)

This is not possible using gsub alone. gsub will always produce a string. If you want the empty string "" to be converted to nil, you will have to write extra logic: if output_string == "" then output_string = nil end

> input_string = "smart" is erroneously converted to output_string = "smart"

Your pattern contains a trailing space. For it to match, your input must contain smart followed by a space. Just smart does not end with a space.

Make the space optional: smart ? will greedily try to match a space, but will also accept no space. Caveat: This will also convert smartass to ass.

Matching end-of-string or a space as would be possible in RegEx using ( |$) is not possible with patterns since alternation (|) is not supported.

Alternatively, you can use extra logic by just handling "smart" explicitly: if input_string == "smart" then output_string = "" end. This will deal properly with smartass, keeping it intact.

Note that you might want %s instead of a literal space to accept other spacing such as tabs. You'll also probably want a quantifier like * to accept multiple space characters. I'd also add the ^ anchor to the beginning of your pattern to not replace smart anywhere in the string.

All in all, I'd recommend output_string = input_string:gsub("^smart%s*", "")


Regarding your update:

> input_string = "smr" or "smr "
>
> I mean any other word, then also I expect output_string = nil
> You could say that all I want is the second word of the string with exactly one space between first & second word. For anything else, I want a null output.

In that case, you definitely want output_string = input_string:match("^smart (.+)").

Explanation: This expects your string to start with smart and then greedily captures the remaining characters till the end. If you want to support an empty word if the string is just "smart ", use .* instead of .+.

If the string does not start with smart , this won't match and the result will be nil.

huangapple
  • 本文由 发表于 2023年5月13日 17:51:43
  • 转载请务必保留本文链接:https://go.coder-hub.com/76242095.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定