Lua:将PCRE转换为Lua

Lua:将PCRE转换为Lua,lua,pcre,Lua,Pcre,我知道Lua没有PCRE。如何将其转换为Lua # Quote shell chars $a =~ s/[\002-\011\013-\032\\\#\?\`\(\)\{\}\[\]\^\*\<\=\>\~\|\; \"\!\$\&\'\202-\377]/\\$&/go; # quote newline as '\n'

我知道Lua没有PCRE。如何将其转换为Lua

# Quote shell chars
$a =~ s/[\002-\011\013-\032\\\#\?\`\(\)\{\}\[\]\^\*\<\=\>\~\|\; \"\!\$\&\'\202-\377]/\\$&/go;
# quote newline as '\n'                                                                             
$a =~ s/[\n]/'\n'/go;
#引用shell chars
$a=~s/[\002-\011\013-\032\\\\\\\\\\\\\\\\\\\\\\\[\]\^*\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\“!!\\\\;
#将换行符引为“\n”
$a=~s/[\n]/'\n'/go;
是否有通用转换器可以将任何PCRE转换为Lua?

您可以使用

# Quote shell chars
$a =~ s/[\002-\011\013-\032\\\#\?\`\(\)\{\}\[\]\^\*\<\=\>\~\|\; \"\!\$\&\'\202-\377]/\\$&/go;
# quote newline as '\n'                                                                             
$a =~ s/[\n]/'\n'/go;
local a = "\002\003\004\005\006\007\008\009\010\011\012\\\n"
res, _ = a:gsub("([\002-\009\011-\026\\#?`(){}%[%]^*<>=~|; \"!$&'\130-\255])", "\\%1")
res, _ = res:gsub("\n", "'\n'")
print(res)
local a=“\002\003\004\005\006\007\008\009\010\011\012\\\n”
res,\=a:gsub(([\002-\009\011-\026\\\\\\\\\\\?`({}%[%])^*=“!$&”\130-\255]),“\\%1”)
res,u=res:gsub(“\n”,“\n”)
打印(res)


请注意,在Lua模式中,
\
不是特殊字符,
%
用于替换特殊字符(如
[
)而
\ddd
转义引用的是十进制代码,而不是八进制代码。

您能解释一下您需要它做什么吗?请注意,Lua模式是用
%
转义的,而不是用
\
转义的。另外,这是一个Perl代码段,不是PCRE。我需要它在VLC for os.execute中引用一个字符串。该字符串可以包含除\0之外的任何字符,并且应该包含不是由shell解释的。请查看。如果它按预期工作,请告知。看起来不错。将其作为答案。
res:gsub(“\n”,“\\n””)
应替换为
res:gsub(“\n”,“\n”)
,如果替换内容应包含
\
,后跟
n
,则是。