gpt4 book ai didi

string - Lua 用 string.gsub 进行普通搜索?

转载 作者:行者123 更新时间:2023-12-02 06:51:26 25 4
gpt4 key购买 nike

使用 Lua 的 string.find 函数,您可以传递一个可选的第四个参数来启用普通搜索。来自 Lua wiki :

The pattern argument also allows more complex searches. See the PatternsTutorial for more information. We can turn off the pattern matching feature by using the optional fourth argument plain. plain takes a boolean value and must be preceeded by index. E.g.,

= string.find("Hello Lua user", "%su")         -- find a space character followed by "u"

10 11

= string.find("Hello Lua user", "%su", 1, true) -- turn on plain searches, now not found

nil

基本上,我想知道如何使用 Lua 的 string.gsub 函数完成相同的简单搜索。

最佳答案

我期望标准库中有一些东西可以用于此目的,但是没有。那么,解决方案就是转义模式中的特殊字符,这样它们就不会执行其通常的功能。

总体思路如下:

  1. 获取模式字符串
  2. 将任何特殊字符替换为 % 后跟它(例如,% 变为 %%[变成 %[
  3. 使用它作为替换文本的搜索模式

关于string - Lua 用 string.gsub 进行普通搜索?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29503721/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com