如何调整Lua模式中符号重复的设置以实现长尾词优化?

2026-04-01 20:221阅读0评论SEO问题
  • 内容介绍
  • 文章标签
  • 相关推荐

本文共计605个文字,预计阅读时间需要3分钟。

如何调整Lua模式中符号重复的设置以实现长尾词优化?

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

我正在寻找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

如何调整Lua模式中符号重复的设置以实现长尾词优化?

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

本文共计605个文字,预计阅读时间需要3分钟。

如何调整Lua模式中符号重复的设置以实现长尾词优化?

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

我正在寻找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

如何调整Lua模式中符号重复的设置以实现长尾词优化?

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