Score:1

Removing specific characters from a string on ansible

lc flag

I have the following

- set_fact:
   test_result: " {{ htmlres.content | regex_search('http://website([0-9]+)', '\\1') }}"

Using debug, this returns the following " '[01]'"

Wanting only the number, I did some experimenting using the replace() function and was able to strip the [ ] by adding the following:

- set_fact:
   test_result: " {{ htmlres.content | regex_search('http://website([0-9]+)', '\\1')
| replace('[','') | replace(']','') }}"

My problem now is that the output is now " '01' and I can't seem to remove the ' or the whitespace.

Adding | trim to the end for some reason doesn't remove the whitespaces, and adding regex_search('\'','') also doesn't seem to escape the character and work.

Am I missing something?

Here's the output from the debug after the first removal:

"msg": [
    " '01'",
    ...

Thanks

Score:1
in flag

You are thinking way too difficult. regex_search() returns an array, and you want the first item.

- set_fact:
    test_result: "{{ htmlres.content | regex_search('http://website([0-9]+)', '\\1') | first }}"

The blank at the start is the result of your assignment of test_result:

    test_result: " {{ htmlres.content ...
                  ^-- here

Just remove it.

Ress avatar
lc flag
Wow I didn't even notice the first space. Also thanks for clarifying that it returns an array. It's matching correctly now! Thanks!
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.