Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Regex 如何使用sed替换字符串中间的匹配字符_Regex_Linux_Bash_Sed - Fatal编程技术网

Regex 如何使用sed替换字符串中间的匹配字符

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-

我有一个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-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为什么我错过了一件简单的事情。