0

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 '/?(.*)'
toydarian
  • 140
  • 1
  • 7
TRONG NGUYEN
  • 3
  • 1
  • 4

1 Answers1

2

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.

toydarian
  • 140
  • 1
  • 7