Hi,

> The task seems obvious at first glance but it appears to be difficult
> to implement in this context.
> The string is:
> host_meta: "<XRD
> xmlns='http://docs.oasis-open.org/ns/xri/xrd-1.0'>\n <Link
> rel='restconf' href='/restconf'/>\n</XRD>"
> 
> I need to extract the values of rel and href into api_release and
> api_root. I unsuccessfully tried the following:
> - set_fact: api_release="{{ host_meta | regex_replace('^.*
> rel=(.*) .*$', '\\1') }}"
>   when: host_meta is defined
> 
> - set_fact: api_root="{{ host_meta | regex_replace('^.*
> href=(.*)/>.*$', '\\1') }}"
>   when: host_meta is defined
> 
> Both variables contain the whole string instead of the corresponding 
> substring, which should be:
> api_release: 'restconf'
> api_root: /restconf
> 
> I have already successfully used this filter in other contexts. 
> What am I missing here? Is the filter confused by the string?

You are missing the newlines the string contains. regex_replace uses
re.sub() in Python and does not offer a way to set the MULTILINE flag.
If you remove '^' and '$' from your regexps, you can see what happens:
only the matching part between two '\n's is replaced.

Instead of regexp_replace() you should use the regex_search() filter;
that works as expected:

  - set_fact: api_release="{{ host_meta | regex_search('rel=(.*)') }}"
    when: host_meta is defined
  - set_fact: api_root="{{ host_meta | regex_search('href=(.*)/>') }}"
    when: host_meta is defined

Yields:

  "api_release": "rel='restconf' href='/restconf'/>"
  "api_root": "href='/restconf'/>"

(That's not exactly equal to what you expected, but that is what your
original regexes would have returned if the multiline flag would have
been set. The problem is that regexes are notoriously bad for matching
XML and or HTML.)

Cheers,
Felix



> 


-- 
Felix Fontein -- fe...@fontein.de -- http://felix.fontein.de/

-- 
You received this message because you are subscribed to the Google Groups 
"Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to ansible-project+unsubscr...@googlegroups.com.
To post to this group, send email to ansible-project@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/ansible-project/20180214163633.41e78214%40utsjoki.
For more options, visit https://groups.google.com/d/optout.

Reply via email to