加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

Lua模式设置中符号的重复量

发布时间:2020-12-14 21:49:58 所属栏目:大数据 来源:网络整理
导读:我正在寻找Lua模式设置中重复符号的数量. 我尝试检查字符串中的符号数量. 正如我在 manual读到的那样, 即使使用字符类,这仍然是非常有限的,因为我们只能匹配具有固定长度的字符串. To solve this,patterns support these four repetition operators: ‘*’ M
我正在寻找Lua模式设置中重复符号的数量.
我尝试检查字符串中的符号数量.
正如我在 manual读到的那样,
即使使用字符类,这仍然是非常有限的,因为我们只能匹配具有固定长度的字符串.

To solve this,patterns support these four repetition operators:

  • ‘*’ Match the previous character (or class) zero or more times,as many times as possible.
  • ‘+’ Match the previous character (or class) one or more times,as many times as possible.
  • ‘-‘ Match the previous character (or class) zero or more times,as few times as possible.
  • ‘?’ Make the previous character (or class) optional.

所以,没有关于大括号的信息{}
例如.,

{1,10}; {1,}; {10};

不起作用.

local np = '1'
local a =  np:match('^[a-zA-Z0-9_]{1}$' )

返回np = nil.

local np = '1{1}'
local a =  np:match('^[a-zA-Z0-9_]{1}$' )

返回np =’1 {1}’:)

这个url说没有这样的魔术符号:

Some characters,called magic characters,have special meanings when
used in a pattern. The magic characters are

06003

卷曲括号仅作为简单文本工作,不再有效.
我对吗?避免这个’错误’的最佳方法是什么?

可以阅读大括号的常用用法,例如here.

解决方法

我们不得不承认Lua正则表达式量词在功能上非常有限.

>他们只是你提到的4个(,–,*和?)
>无限制量词支持(您需要的支持)
>与其他系统不同,在Lua中,修饰符只能应用于字符类;无法在修改器下对模式进行分组(请参阅source). Unfortunately Lua patterns do not support this ('(foo)+' or '(foo|bar)'),only single characters can be repeated or chosen between,not sub-patterns or strings.

作为“解决方法”,为了使用限制量词和所有其他PCRE正则表达式特权,您可以使用rex_pcre library.

或者,正如@moteus建议的那样,部分解决方法是“模拟”具有下限的限制量词,只需重复模式以匹配它几次,并将可用的Lua量词应用于最后一个.例如.匹配3个或更多个模式:

local np = 'abc_123'
local a = np:match('^[a-zA-Z0-9_][a-zA-Z0-9_][a-zA-Z0-9_]+$' )

见IDEONE demo

要考虑的另一个库而不是PCRE是Lpeg.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读