首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >非贪婪和固定数目的字符在VIM中搜索和替换

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

Stack Overflow用户
提问于 2016-05-03 08:38:05
回答 2查看 105关注 0票数 2

我有很多字符串,就像这样-

代码语言:javascript
复制
icon=null restored=0 title=Adblock Browser itemType=0 container=-101

我想替换=之后的每个字符串,以便有一个|分隔两个字段。

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

代码语言:javascript
复制
icon=null |restored=0 |title=Adblock Browser |itemType=0 |container=-101

在VIM中,我尝试了下面的search-and-replace表达式-

代码语言:javascript
复制
:%s/=.\{-} \?.\{-} /\0|/gc

但是,这里的问题是,它与=Adblock Browser不匹配,它只与匹配=Adblock部件。

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

代码语言:javascript
复制
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

输出应该是这样的-

代码语言:javascript
复制
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

其他字符串也是如此。

最小、可验证的例子

这是输入文件的内容-

代码语言:javascript
复制
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中搜索和替换表达式-

代码语言:javascript
复制
:%s/=.\{-} \?.\{-} /\0|/gc

产出:

代码语言:javascript
复制
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

错误部分-

代码语言:javascript
复制
restored=0 |title=Adblock |Browser itemType=0 |container=-101 |

应该是-

代码语言:javascript
复制
restored=0 |title=Adblock Browser |itemType=0 |container=-101 |
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2016-05-03 10:08:10

代码语言:javascript
复制
sed -r 's/ ([^= ]*=)/ |\1/g' file

这将匹配一个空白,后面跟着0或多个字符,除了空白或=,然后是=。()括号中的字符串捕获将在\1 ie第一个捕获组中。\0将在组内捕获所有字符串。

然后用| \1替换匹配

票数 3
EN

Stack Overflow用户

发布于 2016-05-03 10:02:19

我有个坏消息要告诉你:在sed中,量化者是贪婪的。您也没有查找功能,所以不能在sed中执行。

好消息: perl可以帮助您完成这个任务。下面是一个用perl实现几乎所有东西的脚本:

代码语言:javascript
复制
echo "icon=null restored=0 title=Adblock Browser itemType=0 container=-101" |
perl -p -e 's/([^ =]*=)/|\1/g'

不幸的是它会有一个导水管。但这很容易解决-但有点脏,我知道:

代码语言:javascript
复制
echo "icon=null restored=0 title=Adblock Browser itemType=0 container=-101" |
perl -p -e 's/([^ =]*=)/|\1/g' | 
sed 's/^|//g'
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/36999395

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档