Dav*_*vis 5 unix vim regex sed
考虑以下文本:
There are three types of font families: serif, sans serif, and
teletype. To switch between these families, use <cmd>rm</cmd> for
serif, <cmd>ss</cmd> for sans serif, and <cmd>tt</cmd> for teletype.
Run Code Online (Sandbox Code Playgroud)
我想<cmd>x</cmd>改为{{cmd|x}},如下:
There are three types of font families: serif, sans serif, and
teletype. To switch between these families, use {{cmd|rm}} for
serif, {{cmd|ss}} for sans serif, and {{cmd|tt}} for teletype.
Run Code Online (Sandbox Code Playgroud)
非贪婪匹配的正则表达式很棘手。例如,以下在 vim 中不起作用:
:%s/<cmd>\(.*\)<\/cmd>.\{-}/{{cmd|\1}}/
Run Code Online (Sandbox Code Playgroud)
也没有以下内容,使用 sed:
sed -e "/(<cmd>\(.*\)</cmd>).\{-}/{{cmd|\1}}/"
Run Code Online (Sandbox Code Playgroud)
括号尝试匹配括号,而不是将表达式分组以应用\{-}or的非贪婪运算符?。转义括号用于反向引用,只有<cmd>标签内的文本内容才需要。
非贪婪地替换文件中所有出现的<cmd>x</cmd>with的正确语法是什么{{cmd|x}}?
注意:这不是尝试使用正则表达式解析 HTML。;-)
小智 5
我在 VIM: 中尝试了这个%s/<cmd>\(.\{-}\)<\/cmd>/{{cmd|\1}}/g,它将您的演示文本转换为:
There are three types of font families: serif, sans serif, and
teletype. To switch between these families, use {{cmd|rm}} for
serif, {{cmd|ss}} for sans serif, and {{cmd|tt}} for teletype.
Run Code Online (Sandbox Code Playgroud)
看起来你在 VIM 中的第一个正则表达式真的很接近解决你的难题,但是使用的.\{-}地方不正确。
我从这个答案中得到提示:https : //stackoverflow.com/questions/1305853/how-can-i-make-my-match-non-greedy-in-vim