【问题标题】:Ansible replace module regexp starting with '$'Ansible 替换以 '$' 开头的模块正则表达式
【发布时间】:2018-05-21 13:17:55
【问题描述】:

我正在尝试使用 Ansible 中的 replace 模块,但我不知道如何使用 regexp 匹配某些字符串。

我正在尝试匹配以字符 $ 开头的字符串,但 ansible 一直说他是 found unknown escape character '$'

我知道 ansible 使用与 python 相同的正则表达式规则,但我在 python 中也不能这样做,你们知道怎么做吗?

我已经尝试过这些正则表达式规则:

^\$[!^$][!^$]\s*[!^$]

最后 3 条规则匹配以 $ 开头的字符串,但如果字符串不以 $ 开头,则也匹配那些字符串。

最后 3 条规则的一些示例:

foo        doesn't match
$foo       match
$$$$       match
foo$       match
foo$bar    match

我只需要在这种情况下匹配:

foo
$foo       this case
$$$$       this case
foo$
foo$bar

【问题讨论】:

  • 这应该适用于您的情况:'^\$.*$'

标签: python regex python-2.7 replace ansible


【解决方案1】:

使用re.match

演示:

import re
l = ["foo", "$foo", "$$$$", "foo$", "foo$bar"]
for i in l:
    print(re.match("^\$", i))

输出:

None
<_sre.SRE_Match object at 0x0000000001D84578>
<_sre.SRE_Match object at 0x0000000001D84578>
None
None

在 Ansible 中尝试使用 regex_search

【讨论】:

  • 谢谢Rakesh,请问一下,有没有办法在replace模块,regexp字段中使用regex_search?
猜你喜欢
  • 2018-01-16
  • 2017-07-15
  • 1970-01-01
  • 2019-05-01
  • 2021-06-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-10-20
相关资源
最近更新 更多