如何检测字段是否包含Lua中的字符
我正在尝试修改一个现有的lua脚本,该脚本将清理Aegisub中的字幕数据 我想添加删除包含符号“”的行的功能♪" 以下是我要修改的代码:如何检测字段是否包含Lua中的字符,lua,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
-- 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(♪) 则
不是有效的Lua语法
差不多
如果line.comment或line.text==”或line.text:find(“\u{266A}”),则
应该可以工作。
中的仅用于通用for循环。如果line.text在(♪) 则
不是有效的Lua语法
差不多
如果line.comment或line.text==”或line.text:find(“\u{266A}”),则
应该可以工作。在Lua中,每个字符串都有字符串
函数作为附加的方法。
因此,在循环中对字符串变量使用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
被用作进一步处理的条件。在Lua中,每个字符串都有字符串
函数作为附加的方法。
因此,在循环中对字符串变量使用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
被用作进一步处理的条件。in关键字仅用于for循环,如“for i,value in pairs(aTable)do…end”in关键字仅用于for循环,如“for i,value in pairs(aTable)do…end”刚要用string.find(line.text)写入♪)它工作了,只是测试了一下,这与您的建议应该没有什么区别。它工作了!谢谢!我正要用string.find(line.text)编写它♪)它是有效的,只是测试了一下,这与您的建议应该没有什么区别。这很有效!谢谢!