逻辑“或”在 Lua 模式中?

发布于 2024-09-13 22:13:39 字数 469 浏览 3 评论 0原文

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\\", "[\\/]$", "")

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

Is it possible to achieve in 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"

The point here is the no acceptance of logical 'or' statements in Lua patterns.

EDIT:
Just realized that is possible by doing this:

strng.gsub("slash\\ending\\string\\", "[\\/]$", "")

Although logical 'or' for patterns is still missing.

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

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

发布评论

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

评论(4

娇俏 2024-09-20 22:13:39

Lua 不使用标准正则表达式进行模式匹配。引用Programming in Lua一书中的内容解释了原因:

与其他几种脚本语言不同,Lua 不使用 POSIX 正则表达式 (regexp) 进行模式匹配。其主要原因是大小:POSIX regexp 的典型实现需要超过 4,000 行代码。这比所有 Lua 标准库的总和还要大。相比之下,Lua 中模式匹配的实现只有不到 500 行。当然,Lua 中的模式匹配无法完成完整 POSIX 实现所做的所有工作。尽管如此,Lua 中的模式匹配是一个强大的工具,并且包含一些难以与标准 POSIX 实现匹配的功能。

但是,有许多与现有正则表达式库以及高级 LPeg 图书馆。有关它们的列表及其链接,请参阅 http://lua-users.org/wiki/LibrariesAndBindings,章节文本处理

另请参阅此问题:Lua 模式匹配与正则表达式

Lua does not use standard regular expressions for pattern matching. A quote from the book Programming in Lua explains the reason:

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.

However, there are many bindings to existing regular expression libraries and also the advanced LPeg library. For a list of them with links, see http://lua-users.org/wiki/LibrariesAndBindings, chapter Text processing.

Also, see this question: Lua pattern matching vs. regular expressions

梦明 2024-09-20 22:13:39

Lua 模式匹配与正则表达式不同,并且没有交替概念。

例如,如果您想从字符串末尾删除 "abc""efg"(类似于 "(abc|efg)$"< /code> 正则表达式)下面的代码可以很好地工作:

local inputstring="123efgabc"
local s,n = inputstring:gsub("abc$", "")
if n == 0 then
  s,n = inputstring:gsub("efg$", "")
end
print(s) --> 123efg

Lua pattern matching is not the same as regular expressions, and does not have an alternation concept.

For example, if you wanted to remove "abc" or "efg" from the end of a string (similar to "(abc|efg)$" regular expression) the following code would work well:

local inputstring="123efgabc"
local s,n = inputstring:gsub("abc$", "")
if n == 0 then
  s,n = inputstring:gsub("efg$", "")
end
print(s) --> 123efg
落日海湾 2024-09-20 22:13:39

Lua正则表达式……不正常。据我从文档中可以看出,不支持一般交替,也不支持将重复运算符应用于组。在你的情况下,正如你所说,你可以通过字符类获得你想要的东西(不过,我不确定逗号在你的字符类中做什么)。

请参阅此处: http://www.lua.org/manual/5.1 /manual.html#5.4.1

(在我曾经参与的一个项目中,我们因此编写了自己的 Lua 绑定到 PCRE。)

Lua regular expressions are ... abnormal. As far as I can tell from the documentation, there is no support for general alternation, nor for applying repetition operators to groups. In your case, as you say, you can get what you want with a character class (I'm not sure what the comma is doing in your character class, though).

See here: http://www.lua.org/manual/5.1/manual.html#5.4.1

(In a project I used to work on, we wrote our own Lua binding to PCRE because of this.)

夏末染殇 2024-09-20 22:13:39

我自己做了这个,这个函数匹配许多模式并按顺序返回结果,您提供一个模式数组,它按发生的顺序返回出现的数组

示例:

输入字符串:right10down5left9,要匹配的模式:{'up', 'down', 'left', 'right', '%d+' }

返回 {'right', '10', 'down', '5', 'left', '9'}

---@param inputstr string
---@param patternList string[]
---@return string[]
function stringMatchManyPatterns(inputstr, patternList)
    local finalTable = {}
    local keysArray  = {}

    for i = 1, #patternList do
        local patternToMatch = patternList[i]
        for p, str in str.gmatch(inputstr, '()(' .. patternToMatch .. ')') do
            if str ~= nil then
                local key = p + 1
                finalTable[key] = str
                table.insert(keysArray, key)
            end
        end
    end

    table.sort(keysArray)

    local finalArray = {}

    for i = 1, #keysArray do
        table.insert(finalArray, finalTable[keysArray[i]])
    end

    return finalArray
end

I made this myself, this function match many patterns and returns the results in order, you provide an array of patterns and it returns the array of ocurrences in order they happened

Example:

input string: right10down5left9, patterns to match: { 'up' , 'down', 'left', 'right', '%d+' }

returns {'right', '10', 'down', '5', 'left', '9'}

---@param inputstr string
---@param patternList string[]
---@return string[]
function stringMatchManyPatterns(inputstr, patternList)
    local finalTable = {}
    local keysArray  = {}

    for i = 1, #patternList do
        local patternToMatch = patternList[i]
        for p, str in str.gmatch(inputstr, '()(' .. patternToMatch .. ')') do
            if str ~= nil then
                local key = p + 1
                finalTable[key] = str
                table.insert(keysArray, key)
            end
        end
    end

    table.sort(keysArray)

    local finalArray = {}

    for i = 1, #keysArray do
        table.insert(finalArray, finalTable[keysArray[i]])
    end

    return finalArray
end
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文