如何检测字段是否包含Lua中的字符



我正在尝试修改一个现有的lua脚本,该脚本用于清理Aegisub中的字幕数据。

我想添加删除包含符号"的行的能力;♪">

这是我想修改的代码:

-- delete commented or empty lines
function noemptycom(subs,sel)
progress("Deleting commented/empty lines")
noecom_sel={}
for s=#sel,1,-1 do
line=subs[sel[s]]
if line.comment or line.text=="" then
for z,i in ipairs(noecom_sel) do noecom_sel[z]=i-1 end
subs.delete(sel[s])
else
table.insert(noecom_sel,sel[s])
end
end
return noecom_sel
end

我真的不知道我在这里做什么,但我知道一些SQL和LUA显然也使用了IN关键字,所以我尝试将IF行修改为这个

if line.text in (♪) then

不用说,它没有起作用。在LUA中有一种简单的方法可以做到这一点吗?我看到了一些关于string.match((&string.find((函数,但我不知道从哪里开始尝试将这些代码组合在一起。对于一个对Lua一无所知的人来说,最简单的方法是什么?

in仅在泛型for循环中使用。您的if line.text in (♪) then不是有效的Lua语法。

类似的东西

if line.comment or line.text == "" or line.text:find("u{266A}") then

应该有效。

在Lua中,每个字符串都有作为附加方法的string函数
所以在循环中对字符串变量使用gsub(),就像。。。

('Text with ♪ sign in text'):gsub('(♪)','note')

它替换了符号,输出是…

Text with note sign in text

空的"不会将其替换为"note",而是将其删除。
gsub()返回2个值
第一个:有或没有变化的字符串
第二个:一个数字,告诉模式匹配的频率
因此第二个返回值可以用于条件或成功
(0代表"未找到图案"(所以让我们检查上面的。。。

local str,rc=('Text with strange ♪ sign in text'):gsub('(♪)','notation')
if rc~=0 then
print('Replaced ',rc,'times, changed to: ',str)
end
-- output
-- Replaced     1   times, changed to:  Text with strange notation sign in text

最后只检测到,没有做出任何改变。。。

local str,rc=('Text with strange ♪ sign in text'):gsub('(♪)','%1')
if rc~=0 then 
print('Found ',rc,'times, Text is: ',str)
end
-- output is...
-- Found    1   times, Text is:     Text with strange ♪ sign in text

%1保存'(♪)'找到的内容
因此替换为
并且只有rc被用作进一步处理的条件。

最新更新