Lua前沿模式匹配(全词搜索) [英] Lua frontier pattern match (whole word search)

查看:25
本文介绍了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天全站免登陆