Logical 'or' in Lua patterns?
Asked Answered
P

4

19

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.

Poona answered 11/8, 2010 at 20:9 Comment(0)
M
18

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

Mcfadden answered 13/8, 2010 at 9:57 Comment(0)
B
12

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
Bruckner answered 11/8, 2010 at 23:7 Comment(0)
G
3

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.)

Glandular answered 11/8, 2010 at 22:17 Comment(0)
O
0

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
Ortrude answered 13/4 at 23:5 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.