【问题标题】:Remove only character between two known strings仅删除两个已知字符串之间的字符
【发布时间】:2019-10-27 14:48:58
【问题描述】:

我想删除出现在/// 之间的% 符号,但不在此之外。

这是一个例子:

https://delete%me.com/butnot%this
https://donotdelete.me/test

我会使用 sed、grep 或 awk。

【问题讨论】:

  • 欢迎来到 SO,请在您的帖子中的代码标签中添加输入和预期输出示例。还要在帖子中添加您为解决自己的问题所做的努力。

标签: regex awk sed grep


【解决方案1】:

你可以试试这个:

sed 's|\(//[^/]*\)%\([^/]*/\)|\1\2|g'

解释

s            # substitude
|            # separator
\(//[^/]*\)  # pattern start with // --> save in arg1 (\1)
%            # pattern contains %    --> ignore
\([^/]*/\)   # pattern ends with /   --> save in arg2 (\2)
|            # separator
\1\2         # print arg1 and arg2
|            # separator
g            # global on whole line

测试

$ echo "https://delete%me.com/butnot%this" | sed 's|\(//[^/]*\)%\([^/]*/\)|\1\2|g'
https://deleteme.com/butnot%this

$ echo https://donotdelete.me/test | sed 's|\(//[^/]*\)%\([^/]*/\)|\1\2|g'
https://donotdelete.me/test

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-07-01
    • 2013-07-25
    • 1970-01-01
    • 2011-01-30
    • 2020-11-13
    • 2012-05-14
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多