Lua模式设置中符号的重复次数 [英] Amount of repetitions of symbols in Lua pattern setup

查看:608
本文介绍了Lua模式设置中符号的重复次数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找Lua模式设置中符号的重复次数. 我尝试检查字符串中的符号数量. 正如我在手册中所读到的, 即使使用字符类,这仍然是非常有限的,因为我们只能匹配固定长度的字符串.

I'm looking for amount of repetitions of symbols in Lua pattern setup. I try to check amount of symbols in a string. As I read in manual, Even with character classes this is still very limiting, because we can only match strings with a fixed length.

为解决此问题,模式支持以下四个重复运算符:

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.

因此,没有有关大括号{}的信息 例如,

So, no information about Braces {} e.g.,

{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 表示没有这样的魔术符号:

This url says that no such magic symbols:

某些字符(称为魔术字符)在以下情况下具有特殊含义 用于模式.魔术人物是

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

( ) . % + - * ? [ ^ $

弧形括号只能用作简单文本,不能再使用. 我对吗?避免此错误"的最佳方法是什么?

Curly brackets do work only as simple text and no more. Am I right? What is the best way to avoid this 'bug'?

可以读取大括号的常用用法,例如,此处.

It is possible to read usual usage of braces, for instance, here.

推荐答案

我们不得不承认Lua regex量词的功能非常有限.

We can't but admit that Lua regex quantifiers are very limited in functionality.

  1. 它们就是您提到的4个(+-*?)
  2. 无限制的量词支持(您需要的数量)
  3. 与其他系统不同,在Lua中,修饰符只能应用于字符类;没有办法在修饰符下对模式进行分组(请参见). 很不幸,Lua模式不支持此模式('(foo)+''(foo|bar)'),只能使用单个字符在子模式或字符串之间重复或选择.
  1. They are just those 4 you mentioned (+, -, * and ?)
  2. No limiting quantifier support (the ones you require)
  3. Unlike some other systems, in Lua a modifier can only be applied to a character class; there is no way to group patterns under a modifier (see 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 regex特权,可以使用

As a "work-around", in order to use limiting quantifiers and all other PCRE regex perks, you can use rex_pcre library.

或者,如@moteus所建议的,部分解决方法模拟"仅具有下限的限制量词,只需重复该模式以使其匹配几次,然后将可用的Lua量词应用于最后一个一.例如.匹配模式的 3次或多次:

Or, as @moteus suggests, a partial workaround to "emulate" limiting quantifiers having just the lower bound, just repeat the pattern to match it several times and apply the available Lua quantifier to the last one. E.g. to match 3 or more occurrences of a pattern:

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

请参见 IDEONE演示

要考虑的另一个代替PCRE的库是 Lpeg

Another library to consider instead of PCRE is Lpeg.

这篇关于Lua模式设置中符号的重复次数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆