Ansible regex_search module

cn flag

Can anyone please help me to explain this code in ansible regex_search module:

- set_fact:
    regex: "{{ '/opt/conf/path.txt' | regex_search('/?(.*)', '\\1') }}'
  • What "\\1" means in this code.
  • What is the function of each special letter in '/?(.*)'
us flag

This is actually more of a regex-question, not ansible. Apart from that, all information you are looking for is in the python documentation and ansible documentation.

  • \\1 will return the first matching group
  • /?(.*) has several parts
    • /? will match a / if it is there, or nothing if it isn't. The ? means "optional".
    • Everything in brackets () is a matching-group. You can have multiple to get different parts of your match, but here it is only one.
    • .* matches any string composed of any character except new-lines of any length, as . matches any character (except newline) and * means, 0 or more characters.

Check the documentation linked above, they explain all this in detail.

cn flag
thanks so much for your answer, now I got it.

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.