Skip to main content
Asked
Viewed 235 times
0

I'm trying to use replace module from Ansible, and I don't know how to match some string using regexp.

I'm trying to match a string starting with the character $ but ansible keeps saying that he found unknown escape character '$'.

I know that ansible use the same regexp rules from python, but I can't do it in python as well, do you guys know how to do it?

I have tried already these regexp rules:

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

The last 3 rules matches with strings starting with $ but if the string doesn't start with $, matches with those srings too.

some examples for the last 3 rules:

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

I need to match only in this cases:

foo
$foo       this case
$$$$       this case
foo$
foo$bar
1
  • this should work on your case: '^\$.*$' Commented May 21, 2018 at 14:45

1 Answer 1

0

Using re.match

Demo:

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

Output:

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

And in Ansible try using regex_search.

1
  • Thanks Rakesh, let me ask you, there is no way to use regex_search in the replace module, in the regexp field? Commented May 21, 2018 at 14:06

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.

- Stack Overflow">
Skip to main content
Asked
Viewed 235 times
0

I'm trying to use replace module from Ansible, and I don't know how to match some string using regexp.

I'm trying to match a string starting with the character $ but ansible keeps saying that he found unknown escape character '$'.

I know that ansible use the same regexp rules from python, but I can't do it in python as well, do you guys know how to do it?

I have tried already these regexp rules:

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

The last 3 rules matches with strings starting with $ but if the string doesn't start with $, matches with those srings too.

some examples for the last 3 rules:

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

I need to match only in this cases:

foo
$foo       this case
$$$$       this case
foo$
foo$bar
1
  • this should work on your case: '^\$.*$' Commented May 21, 2018 at 14:45

1 Answer 1

0

Using re.match

Demo:

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

Output:

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

And in Ansible try using regex_search.

1
  • Thanks Rakesh, let me ask you, there is no way to use regex_search in the replace module, in the regexp field? Commented May 21, 2018 at 14:06

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.