我正在尝试使用 Salts file.replace state 在 XML 文件中注释掉 import resource="blah.xml"/> 但由于某种原因它似乎不起作用。我已经测试了正则表达式,它找到了要注释掉的导入,我检查了名称路径及其正确性,并且我有 show_changes:true。我是一个刚从学校毕业的初级开发人员,所以我有点不确定这里会发生什么,也不熟悉盐,所以我希望我的解释没问题。
file.replace:
- name: path/file.xml
- pattern: '^\s*\<import resource=\"blah\.xml\"\/\>\s*$'
- repl: '<!-- <import resource="blah.xml"/> -->'
- backup: False
- show_changes: True
<!--
Enable web consoles, REST and Ajax APIs and demos
The web consoles requires by default login, you can disable this in the jetty.xml file
Take a look at ${ACTIVEMQ_HOME}/conf/jetty.xml for more details
-->
<import resource="jetty.xml"/>
</beans>
这看起来正确吗?在那些您可以用来测试您的表达式是否有效的正则表达式网站中,它似乎是。所以除了名称路径之外,我真的想不出还有什么问题......有什么想法吗?
您的模式中似乎有太多转义符。试试这个:
- pattern: '^\s*<import resource="blah\.xml"/>\s*$'
在 Python 中调试:
import re
with open('path/file.xml', 'r') as file:
data = file.read()
pattern = r'^\s*<import resource="blah\.xml"/>\s*$'
match = re.search(pattern, data, flags=re.M)