Regex 如何使用sed替换字符串中间的匹配字符
我有一个XML文件“datasources.XML”Regex 如何使用sed替换字符串中间的匹配字符,regex,linux,bash,sed,Regex,Linux,Bash,Sed,我有一个XML文件“datasources.XML” 需要与此类似的输出 <item-map group="org.base" datasource="localpostgres"/> <item-map group="org.base.pos" datasource="localpostpos"/> <item-map group="org.base.hr" datasource="localposthr"/> 因此,我已通过命令生成输出 sed-
需要与此类似的输出
<item-map group="org.base" datasource="localpostgres"/>
<item-map group="org.base.pos" datasource="localpostpos"/>
<item-map group="org.base.hr" datasource="localposthr"/>
因此,我已通过命令生成输出
sed-e's/datasource=“localderby”/datasource=“localpostgres”/g'-e/datasource=“localderby([a-z])*”/datasource=“localpost*/g”datasources.xml
<item-map group="org.base" datasource="localderby"/>
<item-map group="org.base.pos" datasource="localderbypos"/>
<item-map group="org.base.hr" datasource="localderbyhr"/>
通过第一个表达式,我得到了我想要的“localpostgres”,但第二个表达式缺失。任何人都可以提供帮助,请提供建议?谢谢转发。使用扩展正则表达式(sed-r
)和+
,而不是*
,并分组:
sed -r -e 's/datasource="localderby"/datasource="localpostgres"/g' -e 's/datasource="localderby([a-z]+)"/datasource="localpost\1"/g' datasources.xml
非常感谢Cadrain为什么我错过了一件简单的事情。