北大侠客行MUD论坛

 找回密码
 注册
搜索
热搜: 新手 wiki 升级
查看: 2310|回复: 15

求教,能否在string.find()的第二个参数中实现“或”运算

[复制链接]
发表于 2022-7-1 00:33:35 | 显示全部楼层 |阅读模式
本帖最后由 shenji 于 2022-7-1 12:37 AM 编辑

比如说想做一个存东西的触发

match: ^(你\S+搜出|从\S+身上掉了出来|\S+拿出|\S+拿出了|你\S+起|\S+给你)一(颗|枚)(.*)$
send:if string.find("%3", "★") or string.find("%3", "☆") or string.find("%3", "◎") then
  Send("pack gem")
elseif string.find("%3", "丹") then
  Send("put dan in bao")
elseif string.find("%3", "菩提子") then
  Send("put zi in bao")
end

能否把这两个or的判断放在同一个string.find()中,类似:if string.find("%2", "★" or "☆" or "◎")



北大侠客行Mud(pkuxkx.com),最好的中文Mud游戏!
 楼主| 发表于 2022-7-1 00:35:37 | 显示全部楼层
代码复制粘贴会被吞掉吗。。
北大侠客行Mud(pkuxkx.com),最好的中文Mud游戏!
发表于 2022-7-1 03:22:58 | 显示全部楼层
Lua本质是一个不适合做mud机器的语言的重要原因就是糟糕的文字处理。
因此,做lua机器牢记一点,能用正则的尽量用正则。
北大侠客行Mud(pkuxkx.com),最好的中文Mud游戏!
 楼主| 发表于 2022-7-1 03:28:20 | 显示全部楼层
jarlyyn 发表于 2022-7-1 03:22 AM
Lua本质是一个不适合做mud机器的语言的重要原因就是糟糕的文字处理。
因此,做lua机器牢记一点,能用正则的 ...

好吧,谢谢大佬
话说。。这是起来了还是还没睡呢
北大侠客行Mud(pkuxkx.com),最好的中文Mud游戏!
发表于 2022-7-1 03:31:42 | 显示全部楼层
不能。我今天刚问过了。
北大侠客行Mud(pkuxkx.com),最好的中文Mud游戏!
 楼主| 发表于 2022-7-1 03:33:25 | 显示全部楼层
nox 发表于 2022-7-1 03:31 AM
不能。我今天刚问过了。

sigh
北大侠客行Mud(pkuxkx.com),最好的中文Mud游戏!
发表于 2022-7-1 03:43:37 | 显示全部楼层
Limitations of Lua patterns
Especially if you're used to other languages with regular expressions, you might expect to be able to do stuff like this:

'(foo)+' -- match the string "foo" repeated one or more times
'(foo|bar)' -- match either the string "foo" or the string "bar"
Unfortunately Lua patterns do not support this, only single characters can be repeated or chosen between, not sub-patterns or strings. The solution is to either use multiple patterns and write some custom logic, use a regular expression library like lrexlib or Lua PCRE, or use LPeg [3]. LPeg is a powerful text parsing library for Lua based on Parsing Expression Grammar [4]. It offers functions to create and combine patterns in Lua code, and also a language somewhat like Lua patterns or regular expressions to conveniently create small parsers.

Lua特别说明的,不行。
北大侠客行Mud(pkuxkx.com),最好的中文Mud游戏!
发表于 2022-7-1 09:13:41 | 显示全部楼层
用string.match 可以加正则匹配。自己搜索一下用法吧,不过Lua的正则貌似有点儿奇怪
北大侠客行Mud(pkuxkx.com),最好的中文Mud游戏!
发表于 2022-7-3 06:58:40 | 显示全部楼层
自己写一个就好啦

string.has = function(str, findthis)
    if string.find(findthis, "|") == nil then
        return string.find(str, findthis) ~= nil
    else
        local findall = split(findthis, "|")
        for k, v in pairs(findall) do
            if string.find(str, v) ~= nil then
                return true
            end
        end
        return false
    end
end
北大侠客行Mud(pkuxkx.com),最好的中文Mud游戏!
 楼主| 发表于 2022-7-3 23:50:40 | 显示全部楼层
ppmm 发表于 2022-7-1 09:13 AM
用string.match 可以加正则匹配。自己搜索一下用法吧,不过Lua的正则貌似有点儿奇怪 ...

感谢指路,我找到了lua一个能自定义匹配字符的正则  []

macth: ^你说道:「(.*)」$
send:  
if string.match("%1","[◎,☆,★]") then  ---- string.find()也可以
  Note("匹配成功")
end

实例:
你说道:「☆」
匹配成功

你说道:「★」
匹配成功

你说道:「☆★」
匹配成功
北大侠客行Mud(pkuxkx.com),最好的中文Mud游戏!
您需要登录后才可以回帖 登录 | 注册

本版积分规则

Archiver|手机版|小黑屋|北大侠客行MUD ( 京ICP备16065414号-1 )

GMT+8, 2024-4-23 08:14 PM , Processed in 0.009429 second(s), 14 queries .

Powered by Discuz! X3.4

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表