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

标签 lua lua-patterns

是否有可能在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/

相关文章:

string - 在表中插入字符串的引号和未引号部分

string - Lua中比较字符串是否最多有一个错误字符

ruby - 如何清除redis集群节点的重定向键

c++ - 在 Lua 中注册非静态 C++ 方法

linux - 编译 Lua - 创建 .so 文件?

lua - Corona/Box2D 检测与非移动静态物体的碰撞

lua - Lua 中的函数可以在调用之间存储本地值吗?

Lua 模式 - 如何删除字符串中不需要的字符串

string - 读取字符串时如何匹配 float

Lua:替换一个子串