DebugFactor.com | Forum

Extract a particular word using regex in Ansible

I have a text as below and wanted to extract the patch number AG178492 from the text.

text: “Server 3.6.0.2 Patch for AG178492 Fri Jun 5 20:42:30 PDT 2020”

Please advise.

You can use the below code to extract the patch number.

    set_fact:
      value: "{{text|regex_replace('^Server(.*) Patch for ([\\w\\-]+)(.*)$','\\2')}}"

\\2 extracts the particular word which comes after ‘Patch for’, i.e the patch number AG178492
Additionally, \\1 extracts what ever comes between ‘Server’ and ‘Patch’ i.e the version 3.6.02
\\3 extracts extract all the remaining text, i.e the date Fri Jun 5 20:42:30 PDT 2020