逻辑“或”在 Lua 模式中?
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
Lua 不使用标准正则表达式进行模式匹配。引用Programming in Lua一书中的内容解释了原因:
但是,有许多与现有正则表达式库以及高级 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:
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
Lua 模式匹配与正则表达式不同,并且没有交替概念。
例如,如果您想从字符串末尾删除
"abc"
或"efg"
(类似于"(abc|efg)$"< /code> 正则表达式)下面的代码可以很好地工作:
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: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.)
我自己做了这个,这个函数匹配许多模式并按顺序返回结果,您提供一个模式数组,它按发生的顺序返回出现的数组
示例:
输入字符串:right10down5left9,要匹配的模式:{'up', 'down', 'left', 'right', '%d+' }
返回 {'right', '10', 'down', '5', 'left', '9'}
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'}