String 用于提取/替换//
我有一个字符串,比如String 用于提取/替换//,string,design-patterns,lua,gsub,lua-patterns,String,Design Patterns,Lua,Gsub,Lua Patterns,我有一个字符串,比如hello/world today/ 我需要将/world today/替换为/MY NEW STRING/ 阅读我找到的手册 newString = string.match("hello /world today/","%b//") 我可以用gsub来替换它,但我想知道是否有一种优雅的方法可以只返回/之间的文本,我知道我可以修剪它,但我想知道是否有一种模式。尝试以下方法之一: slashed_text=string.match(“hello/world today/”
hello/world today/
我需要将/world today/
替换为/MY NEW STRING/
阅读我找到的手册
newString = string.match("hello /world today/","%b//")
我可以用
gsub
来替换它,但我想知道是否有一种优雅的方法可以只返回/
之间的文本,我知道我可以修剪它,但我想知道是否有一种模式。尝试以下方法之一:
slashed_text=string.match(“hello/world today/”,“/([^/]*)/”
slashed_text=string.match(“hello/world today/”,“/(.-)/”
slashed_text=string.match(“hello/world today/”,“/(.*)/”
%b/
与/.-/
相比没有任何优势,因为两个分隔符是相同的字符
编辑:添加了lhf建议的模式和更多解释。谢谢,我会试一试。您可能想要
“/(.-)/”
而不是“/(.*)/”
。