# Can we use regex in single-line mode with ansible module like lineinfile \[dot matches all, dotall, re.S, (?s)\]

**URL:** <https://forum.ansible.com/t/can-we-use-regex-in-single-line-mode-with-ansible-module-like-lineinfile-dot-matches-all-dotall-re-s-s/18043>\
**Category:** Ansible Project\
**Created:** [April 21, 2015, 7:21pm UTC](https://forum.ansible.com/t/can-we-use-regex-in-single-line-mode-with-ansible-module-like-lineinfile-dot-matches-all-dotall-re-s-s/18043 "2015-04-21T19:21:00Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![sigml16](https://avatars.discourse-cdn.com/v4/letter/s/c5a1d2/32.png) [@sigml16](https://forum.ansible.com/u/sigml16)\
**Post date:** [April 21, 2015, 7:21pm UTC](https://forum.ansible.com/t/can-we-use-regex-in-single-line-mode-with-ansible-module-like-lineinfile-dot-matches-all-dotall-re-s-s/18043/1 "2015-04-21T19:21:00Z")

</div>

Ansible docs point us on python re web site for regex. In this page, modifiers like ignore case, single line mode are documented, but it seems not supported when used with ansible module where regex is used (like lineinfile)Someone find out how works with (?s) aka single-line mode?

usage: useful to insert line in the rigth place in a file like:

Input:

`foo o o foo bar a r`

regex to insert “foobar” line after “foo” followed by “bar”  
  
 **`(?s)(.*?foo.*?\n)(bar.*)`**

substitution:  
  
 **`\1foobar\n\2`**

Result:

`foo o o foo foobar bar a r`

ref.: [https://docs.python.org/2/library/re.html](https://docs.python.org/2/library/re.html)  
"…  
`(?iLmsux)`

(One or more letters from the set `'i'`, `'L'`, `'m'`, `'s'`, `'u'`, `'x'`.) The group matches the empty string; the letters set the corresponding flags: [`re.I`](https://docs.python.org/2/library/re.html#re.I) (ignore case), [`re.L`](https://docs.python.org/2/library/re.html#re.L) (locale dependent), [`re.M`](https://docs.python.org/2/library/re.html#re.M) (multi-line), [`re.S`](https://docs.python.org/2/library/re.html#re.S) (dot matches all), [`re.U`](https://docs.python.org/2/library/re.html#re.U) (Unicode dependent), and [`re.X`](https://docs.python.org/2/library/re.html#re.X) (verbose), for the entire regular expression. (The flags are described in [_Module Contents_](https://docs.python.org/2/library/re.html#contents-of-module-re).) This is useful if you wish to include the flags as part of the regular expression, instead of passing a _flag_ argument to the [`re.compile()`](https://docs.python.org/2/library/re.html#re.compile) function.

…"

Ansible should support this since is build-in in python and look very easy to support.

(Note: I am pretty new with ansible, python and i am not english. So, no bashing will be apreciated… i am looking for a solution not useless comments)

---

<div class="post-metadata">

**Author:** ![sigml16](https://avatars.discourse-cdn.com/v4/letter/s/c5a1d2/32.png) [@sigml16](https://forum.ansible.com/u/sigml16)\
**Post date:** [April 22, 2015, 2:07pm UTC](https://forum.ansible.com/t/can-we-use-regex-in-single-line-mode-with-ansible-module-like-lineinfile-dot-matches-all-dotall-re-s-s/18043/2 "2015-04-22T14:07:57Z")

</div>

**UPDATE** : I tested the re.IGNORECASE inline modifier (?i) and this one work well. I dont know why the single line modifier (?s) don’t works then.

---

<div class="post-metadata">

**Author:** ![Brian\_Coca](https://avatars.discourse-cdn.com/v4/letter/b/6de8d8/32.png) [@Brian\_Coca](https://forum.ansible.com/u/Brian_Coca)\
**Post date:** [April 22, 2015, 2:28pm UTC](https://forum.ansible.com/t/can-we-use-regex-in-single-line-mode-with-ansible-module-like-lineinfile-dot-matches-all-dotall-re-s-s/18043/3 "2015-04-22T14:28:04Z")

</div>

lineinfile applies the regex to single lines already, it does not  
apply it against the whole document, so adding single line parsing is  
useless.

---

<div class="post-metadata">

**Author:** ![sigml16](https://avatars.discourse-cdn.com/v4/letter/s/c5a1d2/32.png) [@sigml16](https://forum.ansible.com/u/sigml16)\
**Post date:** [April 22, 2015, 4:50pm UTC](https://forum.ansible.com/t/can-we-use-regex-in-single-line-mode-with-ansible-module-like-lineinfile-dot-matches-all-dotall-re-s-s/18043/4 "2015-04-22T16:50:57Z")

</div>

[SOLVED]  
ALL GOOD, i discover than “replace” module works well with DOTALL flag.
