【发布时间】:2016-08-15 19:07:28
【问题描述】:
我想使用一个匹配和替换的正则表达式 + backref 表达式
text = 'a,b,c' 与 text = 'a,b,c,item_1'
text = '' 与 text = 'item_1' 和
text = 'a' 和 text = 'a,item_1'。
我通常对使用
sed以及使用 python regex 的解决方案感兴趣。特别是,我正在寻找与lineinfile 模块Ansible (python) 一起使用的解决方案。
这是我目前所拥有的(在 Ansible 中):
regexp: "^(text[ ]*=[ ]*')([^']*)(')"
backrefs: yes
line: '\1item_1,\2\3'
编辑:如果可能,正则表达式 + backref-ex 对应忽略已经存在的 item_1,即“替换”
text = 'item_1' 与 text = 'item_1' 和
text = 'a,item_1' 与 text = 'a,item_1' 和
text = 'a,d,x' 与 text = 'a,item_1,x' 等
【问题讨论】:
-
a、b 和 c 是否总是单个字母,或者可以是任何字母吗?
-
在实践中,列表中的项目可以是带有下划线的真实单词,但对于这个问题,让我们保留单个字符。
标签: python regex sed ansible backreference