Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/lua/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Lua模式帮助(双括号)_Lua_Lua Patterns - Fatal编程技术网

Lua模式帮助(双括号)

Lua模式帮助(双括号),lua,lua-patterns,Lua,Lua Patterns,我一直在用Lua编写一个程序,自动格式化角色扮演中的IRC日志。在角色扮演日志中,有一个关于“性格不正常”对话的特定指南,我们使用双括号来表示。例如:(())。我一直试图让我的程序删除双括号(包括两个括号)之间的文本。代码是: ofile = io.open("Output.txt", "w") rfile = io.open("Input.txt", "r") p = rfile:read("*all") w = string.gsub(p, "%(%(.*?%)%)", "") ofile:

我一直在用Lua编写一个程序,自动格式化角色扮演中的IRC日志。在角色扮演日志中,有一个关于“性格不正常”对话的特定指南,我们使用双括号来表示。例如:
(())
。我一直试图让我的程序删除双括号(包括两个括号)之间的文本。代码是:

ofile = io.open("Output.txt", "w")
rfile = io.open("Input.txt", "r")
p = rfile:read("*all")
w = string.gsub(p, "%(%(.*?%)%)", "")
ofile:write(w)
这里的模式是>
“%((.*?)%”)“
我尝试了该模式的多种变体。所有这些都导致了毫无结果的结果:

1. %(%(.*?%)%) --Wouldn't do anything.
2. %(%(.*%)%) --Would remove *everything* after the first OOC message.
然后,我的朋友告诉我,在括号前加上百分比是行不通的,我必须用反斜杠来“转义”括号

3. \(\(.*\)\) --resulted in the output file being completely empty.
4. (\(\(.*\)\)) --Same result as above.
5. (\(\(.*?\)\) --would for some reason, remove large parts of the text for no apparent reason.
6. \(\(.*?\)\) --would just remove all the text except for the last line.
简短而绝对的问题:
我需要使用什么模式来删除双括号之间的所有文本,并删除双括号本身?

您的朋友正在考虑正则表达式。Lua模式相似,但不同<代码>%是正确的转义字符


您的模式应为
%(.-%)%
-
*
相似,因为它匹配前面序列中的任意数量,但是当
*
尝试匹配尽可能多的字符时(它是贪婪的),
-
匹配尽可能少的字符(它是非贪婪的)。它不会过火,也不会匹配额外的双右括号。

这些是正则表达式,而不是设计模式。