Lua边界模式匹配(全字搜索) [英] Lua frontier pattern match (whole word search)

查看:485
本文介绍了Lua边界模式匹配(全字搜索)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人可以帮我吗?

s_test = "this is a test string this is a test string "

function String.Wholefind(Search_string, Word)
 _, F_result = string.gsub(Search_string, '%f[%a]'..Word..'%f[%A]',"")
 return F_result
end

A_test = String.Wholefind(s_test,"string")
output: A_test = 2

因此边界模式可以找到整个单词,而gsub可以计算整个单词,但是如果搜索字符串中包含数字怎么办?

So the frontier pattern finds the whole word no problem and gsub counts the whole words no problem but what if the search string has numbers?

s_test = " 123test 123test 123"
B_test = String.Wholefind(s_test,"123test")
output: B_test = 0 

如果数字不在搜索字符串的开头或结尾,则似乎可以使用

seems to work with if the numbers aren't at the start or end of the search string

推荐答案

您的模式不匹配,因为您正在尝试做不可能的事情.

Your pattern doesn't match because you are trying to do the impossible.

包括变量值后,模式如下所示:%f[%a]123test%f[%A].意思是:

After including your variable value, the pattern looks like this: %f[%a]123test%f[%A]. Which means:

  1. %f [%a]-查找从非字母到字母的过渡
  2. 123-在从非字母转换为字母后的位置找到123.这本身是一种逻辑上的不可能,因为当非字母跟随在字母上时,您无法将其转换为字母.

您的模式(按书面规定)不适用于以非字母开头或结尾的任何单词.

Your pattern (as written) will not work for any word that starts or ends with a non-letter.

如果需要搜索包含字母和数字的片段,则需要将模式更改为类似'%f[%S]'..Word..'%f[%s]'的形式.

If you need to search for fragments that include letters and numbers, then your pattern needs to be changed to something like '%f[%S]'..Word..'%f[%s]'.

这篇关于Lua边界模式匹配(全字搜索)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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