Reputation: 2545
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.
Upvotes: 20
Views: 22259
Reputation: 1
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
Upvotes: 0
Reputation: 16753
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
Upvotes: 18
Reputation: 2753
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
Upvotes: 12
Reputation: 140540
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.)
Upvotes: 4