正则表达式模式替换

标签 regex bash shell unix

所以我想替换以下内容

<duration>89</duration>

与 (预期结果或者至少应该变成这样:)

\n<duration>89</duration>

所以基本上用正则表达式中的\n< 替换每个 < 所以我想。

sed -e 's/<[^/]/\n</g'

它明显输出的唯一问题

\n<uration>89</duration>

这引出了我的问题。我怎样才能告诉正则表达式匹配<(不是/)后面的字符,但阻止它替换它,以便我可以获得预期的结果?

最佳答案

试试这个:

sed -e 's/<[^/]/\\n&/g' file

sed -e 's/<[^/]/\n&/g' file

&: refer to that portion of the pattern space which matched

关于正则表达式模式替换,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37908292/

相关文章:

ruby - 如何扫描文本中的多个字符串?

regex - 惰性可选量词应该如何表现?

linux - 从 Bash 获取文件的直接链接?

python - 如何从 python 脚本中执行 python 脚本

linux - 在后台启动进程,获取 PID,并在 Shell 中写入标准输入

javascript - PHP 或 Javascript - 查找特定字符串

javascript - 带反向引用的正则表达式捕获组

javascript - MongoDB - 不一致的游标行为

Linux 用户列表和 UID/bash 层

bash - 如何将变量设置为 Bash 中命令的输出?