gpt4 book ai didi

lua - Lua模式中的逻辑 'or'?

转载 作者:行者123 更新时间:2023-12-04 01:58:01 62 4
gpt4 key购买 nike

是否有可能在Lua中实现?

local noSlashEnding = string.gsub("slash\\ending\\string\\", "\\|/$", "")
-- noSlashEnding should contain "slash\\ending\\string"

local noSlashEnding2 = string.gsub("slash/ending/string/", "\\|/$", "")
-- noSlashEnding2 should contain "slash/ending/string"

这里的重点是不接受 Lua 模式中的逻辑“或”语句。

编辑:
刚刚意识到这样做是可能的:
strng.gsub("slash\\ending\\string\\", "[\\/]$", "")

尽管仍然缺少模式的逻辑“或”。

最佳答案

Lua 不使用标准正则表达式进行模式匹配。引用本书 Programming in Lua解释原因:

Unlike several other scripting languages, Lua does not use POSIX regular expressions (regexp) for pattern matching. The main reason for this is size: A typical implementation of POSIX regexp takes more than 4,000 lines of code. This is bigger than all Lua standard libraries together. In comparison, the implementation of pattern matching in Lua has less than 500 lines. Of course, the pattern matching in Lua cannot do all that a full POSIX implementation does. Nevertheless, pattern matching in Lua is a powerful tool and includes some features that are difficult to match with standard POSIX implementations.



但是,现有的正则表达式库和高级 LPeg 有很多绑定(bind)。图书馆。有关它们的链接列表,请参阅 http://lua-users.org/wiki/LibrariesAndBindings , 章 Text processing .

另外,请参阅以下问题: Lua pattern matching vs. regular expressions

关于lua - Lua模式中的逻辑 'or'?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3462370/

62 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com