Regex 非贪婪和固定数量的字符在VIM中搜索和替换

Regex 非贪婪和固定数量的字符在VIM中搜索和替换,regex,vim,sed,Regex,Vim,Sed,我有很多类似这样的字符串- icon=null restored=0 title=Adblock Browser itemType=0 container=-101 icon=null profileId=0 screen=0 modified=1462258474716 iconPackage=null iconResource=null spanX=1 cellX=2 displayMode=null appWidgetProvider=null intent=#Intent;action

我有很多类似这样的字符串-

icon=null restored=0 title=Adblock Browser itemType=0 container=-101
icon=null profileId=0 screen=0 modified=1462258474716 iconPackage=null iconResource=null spanX=1 cellX=2 displayMode=null appWidgetProvider=null intent=#Intent;action=android.intent.action.MAIN;category=android.intent.category.LAUNCHER;launchFlags=0x10200000;component=com.google.android.play.games/com.google.android.gms.games.ui.destination.main.MainActivity;end restored=0 title=Play Games itemType=0 container=8 iconType=null isShortcut=null spanY=1 _id=14 cellY=1 uri=null appWidgetId=-1
icon=null profileId=0 screen=1 modified=0 iconPackage=null iconResource=null spanX=1 cellX=3 displayMode=null appWidgetProvider=null intent=#Intent;action=android.intent.action.MAIN;category=android.intent.category.LAUNCHER;launchFlags=0x10200000;component=com.android.vending/.AssetBrowserActivity;end restored=0 title=Play Store itemType=0 container=-100 iconType=null isShortcut=null spanY=1 _id=15 cellY=3 uri=null appWidgetId=-1
icon=null profileId=0 screen=4 modified=0 iconPackage=null iconResource=null spanX=1 cellX=4 displayMode=null appWidgetProvider=null intent=#Intent;action=android.intent.action.MAIN;category=android.intent.category.LAUNCHER;launchFlags=0x10200000;package=org.adblockplus.browser;component=org.adblockplus.browser/.App;end restored=0 title=Adblock Browser itemType=0 container=-101 iconType=null isShortcut=null spanY=1 _id=19 cellY=0 uri=null appWidgetId=-1
icon=null |profileId=0 |screen=0 |modified=1462258474716 |iconPackage=null |iconResource=null |spanX=1 |cellX=2 |displayMode=null |appWidgetProvider=null |intent=#Intent;action=android.intent.action.MAIN;category=android.intent.category.LAUNCHER;launchFlags=0x10200000;component=com.google.android.play.games/com.google.android.gms.games.ui.destination.main.MainActivity;end |restored=0 |title=Play Games |itemType=0 container=8 |iconType=null |isShortcut=null |spanY=1 |_id=14 |cellY=1 |uri=null |appWidgetId=-1
我想替换
=
后面的每个字符串,这样就有一个
分隔两个字段

在上面的示例中,我希望将字符串替换为-

icon=null |restored=0 |title=Adblock Browser |itemType=0 |container=-101

在VIM中,我尝试了以下
搜索和替换
表达式-

:%s/=.\{-} \?.\{-} /\0|/gc
但是,这里的问题是,它不匹配
=Adblock Browser
,它只匹配
=Adblock
部分


关于我使用
\{-}
的部分,是因为有时字符串是这样的-

icon=null restored=0 title=Adblock Browser itemType=0 container=-101
icon=null profileId=0 screen=0 modified=1462258474716 iconPackage=null iconResource=null spanX=1 cellX=2 displayMode=null appWidgetProvider=null intent=#Intent;action=android.intent.action.MAIN;category=android.intent.category.LAUNCHER;launchFlags=0x10200000;component=com.google.android.play.games/com.google.android.gms.games.ui.destination.main.MainActivity;end restored=0 title=Play Games itemType=0 container=8 iconType=null isShortcut=null spanY=1 _id=14 cellY=1 uri=null appWidgetId=-1
icon=null profileId=0 screen=1 modified=0 iconPackage=null iconResource=null spanX=1 cellX=3 displayMode=null appWidgetProvider=null intent=#Intent;action=android.intent.action.MAIN;category=android.intent.category.LAUNCHER;launchFlags=0x10200000;component=com.android.vending/.AssetBrowserActivity;end restored=0 title=Play Store itemType=0 container=-100 iconType=null isShortcut=null spanY=1 _id=15 cellY=3 uri=null appWidgetId=-1
icon=null profileId=0 screen=4 modified=0 iconPackage=null iconResource=null spanX=1 cellX=4 displayMode=null appWidgetProvider=null intent=#Intent;action=android.intent.action.MAIN;category=android.intent.category.LAUNCHER;launchFlags=0x10200000;package=org.adblockplus.browser;component=org.adblockplus.browser/.App;end restored=0 title=Adblock Browser itemType=0 container=-101 iconType=null isShortcut=null spanY=1 _id=19 cellY=0 uri=null appWidgetId=-1
icon=null |profileId=0 |screen=0 |modified=1462258474716 |iconPackage=null |iconResource=null |spanX=1 |cellX=2 |displayMode=null |appWidgetProvider=null |intent=#Intent;action=android.intent.action.MAIN;category=android.intent.category.LAUNCHER;launchFlags=0x10200000;component=com.google.android.play.games/com.google.android.gms.games.ui.destination.main.MainActivity;end |restored=0 |title=Play Games |itemType=0 container=8 |iconType=null |isShortcut=null |spanY=1 |_id=14 |cellY=1 |uri=null |appWidgetId=-1
输出应该是这样的-

icon=null restored=0 title=Adblock Browser itemType=0 container=-101
icon=null profileId=0 screen=0 modified=1462258474716 iconPackage=null iconResource=null spanX=1 cellX=2 displayMode=null appWidgetProvider=null intent=#Intent;action=android.intent.action.MAIN;category=android.intent.category.LAUNCHER;launchFlags=0x10200000;component=com.google.android.play.games/com.google.android.gms.games.ui.destination.main.MainActivity;end restored=0 title=Play Games itemType=0 container=8 iconType=null isShortcut=null spanY=1 _id=14 cellY=1 uri=null appWidgetId=-1
icon=null profileId=0 screen=1 modified=0 iconPackage=null iconResource=null spanX=1 cellX=3 displayMode=null appWidgetProvider=null intent=#Intent;action=android.intent.action.MAIN;category=android.intent.category.LAUNCHER;launchFlags=0x10200000;component=com.android.vending/.AssetBrowserActivity;end restored=0 title=Play Store itemType=0 container=-100 iconType=null isShortcut=null spanY=1 _id=15 cellY=3 uri=null appWidgetId=-1
icon=null profileId=0 screen=4 modified=0 iconPackage=null iconResource=null spanX=1 cellX=4 displayMode=null appWidgetProvider=null intent=#Intent;action=android.intent.action.MAIN;category=android.intent.category.LAUNCHER;launchFlags=0x10200000;package=org.adblockplus.browser;component=org.adblockplus.browser/.App;end restored=0 title=Adblock Browser itemType=0 container=-101 iconType=null isShortcut=null spanY=1 _id=19 cellY=0 uri=null appWidgetId=-1
icon=null |profileId=0 |screen=0 |modified=1462258474716 |iconPackage=null |iconResource=null |spanX=1 |cellX=2 |displayMode=null |appWidgetProvider=null |intent=#Intent;action=android.intent.action.MAIN;category=android.intent.category.LAUNCHER;launchFlags=0x10200000;component=com.google.android.play.games/com.google.android.gms.games.ui.destination.main.MainActivity;end |restored=0 |title=Play Games |itemType=0 container=8 |iconType=null |isShortcut=null |spanY=1 |_id=14 |cellY=1 |uri=null |appWidgetId=-1
和其他字符串类似


最小的、可验证的示例 这是输入文件的内容-

icon=null profileId=0 screen=4 modified=0 iconPackage=null iconResource=null spanX=1 cellX=4 displayMode=null appWidgetProvider=null intent=#Intent;action=android.intent.action.MAIN;category=android.intent.category.LAUNCHER;launchFlags=0x10200000;package=org.adblockplus.browser;component=org.adblockplus.browser/.App;end restored=0 title=Adblock Browser itemType=0 container=-101 iconType=null isShortcut=null spanY=1 _id=19 cellY=0 uri=null appWidgetId=-1
VIM中的搜索和替换表达式-

:%s/=.\{-} \?.\{-} /\0|/gc
输出:

icon=null |profileId=0 |screen=4 |modified=0 |iconPackage=null |iconResource=null |spanX=1 |cellX=4 |displayMode=null |appWidgetProvider=null |intent=#Intent;action=android.intent.action.MAIN;category=android.intent.category.LAUNCHER;launchFlags=0x10200000;package=org.adblockplus.browser;component=org.adblockplus.browser/.App;end |restored=0 |title=Adblock |Browser itemType=0 |container=-101 |iconType=null |isShortcut=null |spanY=1 |_id=19 |cellY=0 |uri=null |appWidgetId=-1
错误部分-

restored=0 |title=Adblock |Browser itemType=0 |container=-101 |
本应是—

restored=0 |title=Adblock Browser |itemType=0 |container=-101 |

我有一些坏消息要告诉你:在sed中,定量者是贪婪的。您也没有lookaround,因此无法在sed中执行此操作

好消息:perl可以帮助您实现这一点。下面是一个用perl实现几乎所有功能的脚本:

echo "icon=null restored=0 title=Adblock Browser itemType=0 container=-101" |
perl -p -e 's/([^ =]*=)/|\1/g'
不幸的是,它会有一个引导管道。但这很容易修复,但有点脏,我知道:

echo "icon=null restored=0 title=Adblock Browser itemType=0 container=-101" |
perl -p -e 's/([^ =]*=)/|\1/g' | 
sed 's/^|//g'
这将匹配一个空格,后跟0个或更多字符,除了空格或
=
,然后后跟
=
。 ()括号内捕获的字符串将首先位于
\1
ie中。
\0
将在组内捕获所有字符串。

然后用
|\1

sed的/[a-zA-Z]*=/|和/g'文件替换匹配项
解决了这个问题,只是它在一开始就添加了一个
。@fedorqui正如我在问题的第二部分中提到的,字符串不是很简单。例如,
intent=#intent;action=android.intent.action.MAIN;category=android.intent.category.LAUNCHER;launchFlags=0x10200000;component=com.google.android.play.games/com.google.android.gms.games.ui.destination.main.main活动;结束
除了正常的
[a-zA-Z]
字符和添加
之外,还有许多其他字符或``把整个事情搞砸了。好吧,我明白了。如果你能展示一个涵盖所有这些情况的答案,以及预期的结果,那就太好了。@fedorqui问题有导致错误的给定输入以及错误部分。不管怎样,我已经用一个最小的可验证的例子更新了这个问题。这个怎么样:
:%s/\s\zs\ze\s\+=/|/g
这个有效。你能解释一下正则表达式吗。另外,
\0
\1
等之间的区别是什么。因此,您匹配了
标题=
,而不是
=Adblock Browser
。这很聪明。