模式匹配中的lua变量
我只是想知道是否可以在 Lua 中将变量放入模式匹配中。就像类似下面的内容:
var = "hello"
pattern = string.match(datasource, "(var)%s(a%+)")
我需要这样做的原因是因为变量“var”会定期更改。 (它将循环)
提前干杯
Im just wondering if it is possible to put a variable in a pattern match in Lua. Like something similar to the following:
var = "hello"
pattern = string.match(datasource, "(var)%s(a%+)")
The reason I need to do this is because the variable "var" will change periodically. (it will be in a loop)
Cheers in advance
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
Lua 不处理引号内的字符串插值。相反,您需要将这些部分与 var 连接起来作为 var 引用,其余部分作为引号字符串。
"("..var..")%s(a%+)"
以“(”作为字符串文字开头,连接变量,然后用字符串文字。Lua doesn't handle string interpolation inside of the quotes. Instead, you'll need to concatenate the parts with the var as a var reference and the rest as quote strings.
"("..var..")%s(a%+)"
starts with a "(" as a string literal, concatenates the variable, then finishes off the rest of the string with a string literal.请改用
"("..var..")%s(a%+)"
。Use
"("..var..")%s(a%+)"
instead.我需要同样的东西,我认为,模式匹配中的变量,但上述解决方案对我不起作用。我发布我的解决方案,以防它对某人有帮助,但在网上没有找到类似的东西。
我读取了一个以“:”分隔的文件(名称:电话),并希望按文件中的名称进行搜索,并将名称和电话号码作为答案。
I needed the same thing I think, a variable in a pattern match, but the above solution didn't work for me. I'm posting my solution in case it helps someone, didn't find anything else on the net like it.
I read a ': ' delimited file (name: tel) and want to search by name in the file and have the name and telephone number as answer.