【发布时间】:2018-08-22 02:05:00
【问题描述】:
我有一个包含这一行的 config.xml 文件:
<widget id="com.FitDegree.SOMETHING" version="5.1.0" xmlns="http://www.w3.org/ns/widgets" xmlns:cdv="http://cordova.apache.org/ns/1.0">
使用 bash 脚本,我需要将 com.FitDegree.SOMETHING 替换为字符串,例如 com.FitDegree.ThisIsIt
我能得到的最接近的是:
sed -r 's/\"com\.FitDegree\..+?\"/"com.FitDegree.ThisIsIt"/' ../config.xml > tmpfile
mv tmpfile ../config.xml
但结果是:
<widget id="com.FitDegree.ThisIsIt">
注意:它去掉了该行中的所有其他内容,例如版本、xmlns 等。
当我在正则表达式测试器上测试它时:https://regex101.com/r/nI8xB8/1 它只选择 com.FitDegree.SOMETHING
有什么办法解决这个问题吗?
【问题讨论】:
-
这是 xmlstarlet 的工作。
-
sed不知道如何进行非贪婪匹配。你可能想要一个像这样的字符类:'s/\"com\.FitDegree\.[^"]+\"/"com.FitDegree.ThisIsIt"/' -
@0x5453 这就是解决方案!谢谢 - 将此作为正确答案,我会将其标记为正确
标签: regex xml bash sed xml-namespaces