regex fails to match ^, $, \b in multiline mode

256 views
Skip to first unread message

Dennis M

unread,
Aug 30, 2021, 4:37:53 AM8/30/21
to Ansible Project
Hi,
I'm studying how to use regexp, and just follow documented examples,
{{ 'foo\nBAR' | regex_search("^bar", multiline=True, ignorecase=True) }}
expected result is: "bar".
Instead of "bar" it gets an empty string.
Is it a bug, or do I miss an important setting?

Thanks,
Dennis

Antony Stone

unread,
Aug 30, 2021, 4:50:44 AM8/30/21
to ansible...@googlegroups.com
See https://docs.ansible.com/ansible/latest/user_guide/playbooks_filters.html
#searching-strings-with-regular-expressions

and pay careful attention to details such as punctuation and case:

1. The example is:
{{ 'foo\nBAR' | regex_search('^bar', multiline=True, ignorecase=True) }}
instead, you used:
{{ 'foo\nBAR' | regex_search("^bar", multiline=True, ignorecase=True) }}

2. The expected result is:
# => 'BAR'
whereas you say:
expected result is: "bar".

Try exactly what the example shows, and let us know if it does not produce
what the example should produce.


Antony.

--
What is this talk of "software release"?
Our software evolves and matures until it is capable of escape, leaving a
bloody trail of designers and quality assurance people in its wake.

Please reply to the list;
please *don't* CC me.

Dennis M

unread,
Aug 31, 2021, 2:43:57 AM8/31/21
to Ansible Project
Hi,

BTW, ansible-2.9.24-2.el7.

"bar" instead of "BAR" was just a mistype.
Double quote instead of single quote plays no role, i.e. this YAML:

- hosts: monitor1p
  tasks:
  - debug:
      msg: Debug> {{ 'foo\nBAR' | regex_search('^bar', multiline=True, ignorecase=True) }}

produces same output:

ok: [monitor1p] => {
    "msg": "Debug> "
}

Regards,


понедельник, 30 августа 2021 г. в 11:50:44 UTC+3, Antony Stone:

Dick Visser

unread,
Aug 31, 2021, 3:44:19 AM8/31/21
to ansible...@googlegroups.com
You need to quote the entire jinja expression itself (including the
curlies) if you use it as a yaml value.
Because the single quotes are already using inside the expression,
you;'d have to use the double quotes:

tasks:
- debug:
msg: "Debug> {{ 'foo\nBAR' | regex_search('^bar',
multiline=True, ignorecase=True) }}"


> --
> 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-proje...@googlegroups.com.
> To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/8852b14e-1080-4383-a150-5cc32f545815n%40googlegroups.com.



--
Dick Visser
Trust & Identity Service Operations Manager
GÉANT
Reply all
Reply to author
Forward
0 new messages