# Replace indented string in file

**URL:** <https://forum.ansible.com/t/replace-indented-string-in-file/36186>\
**Category:** Ansible Project\
**Created:** [January 10, 2022, 12:03pm UTC](https://forum.ansible.com/t/replace-indented-string-in-file/36186 "2022-01-10T12:03:55Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Tony1](https://avatars.discourse-cdn.com/v4/letter/t/858c86/32.png) [@Tony1](https://forum.ansible.com/u/Tony1)\
**Post date:** [January 10, 2022, 12:03pm UTC](https://forum.ansible.com/t/replace-indented-string-in-file/36186/1 "2022-01-10T12:03:55Z")

</div>

I am trying to replace a specific part of an indented string, but I cannot get it working with lineinfile/replace modules for some reason.

What I would like:

before: Substitute “s~(src|href)=('|")/((?!version).\*)~$1=$2/ **version** //$3~i”

after: Substitute “s~(src|href)=('|")/((?!version).\*)~$1=$2/ **9.1.2.3** /$3~i”

Anyone help me out with this one please?

Cheers!

---

<div class="post-metadata">

**Author:** ![Dick\_Visser](https://avatars.discourse-cdn.com/v4/letter/d/d9b06d/32.png) [@Dick\_Visser](https://forum.ansible.com/u/Dick_Visser)\
**Post date:** [January 10, 2022, 8:54pm UTC](https://forum.ansible.com/t/replace-indented-string-in-file/36186/2 "2022-01-10T20:54:32Z")

</div>

Are those actual entire lines of content?  
Because they contain regex patterns.

---

<div class="post-metadata">

**Author:** ![Tony1](https://avatars.discourse-cdn.com/v4/letter/t/858c86/32.png) [@Tony1](https://forum.ansible.com/u/Tony1)\
**Post date:** [January 11, 2022, 8:36am UTC](https://forum.ansible.com/t/replace-indented-string-in-file/36186/3 "2022-01-11T08:36:56Z")

</div>

Hey,  
Are those actual entire lines of content?

**Yes, they are the enter lines of the content. It is part of an application configuration file.**

**Because they contain regex patterns.**  
Yes and I think this is why I cant get it working

Any pointers would be great, I just need to insert a replace “version” with a version number i.e. 9.1.2.3

---

<div class="post-metadata">

**Author:** ![Dick\_Visser](https://avatars.discourse-cdn.com/v4/letter/d/d9b06d/32.png) [@Dick\_Visser](https://forum.ansible.com/u/Dick_Visser)\
**Post date:** [January 11, 2022, 11:53am UTC](https://forum.ansible.com/t/replace-indented-string-in-file/36186/4 "2022-01-11T11:53:35Z")

</div>

I think you misunderstood me.  
Are those lines you put there the same as in your source file?  
Because I think they’re not.

Instead of posting what appear to be randomly selected lines of a playbook:

- post the actual playbook/task that you have
- post a snippet of the real file content that you want to change

---

<div class="post-metadata">

**Author:** ![Tony1](https://avatars.discourse-cdn.com/v4/letter/t/858c86/32.png) [@Tony1](https://forum.ansible.com/u/Tony1)\
**Post date:** [January 11, 2022, 4:30pm UTC](https://forum.ansible.com/t/replace-indented-string-in-file/36186/5 "2022-01-11T16:30:29Z")

</div>

I have a file called “bla.conf” which contains this indented line Substitute “s~(src|href)=('|")/((?!version).\*)~$1=$2/ **version** //$3~i”

I want to change the indented line from bla.conf to Substitute “s~(src|href)=('|")/((?!version).\*)~$1=$2/ **NEWVERSIONHERE** //$3~i”

---

<div class="post-metadata">

**Author:** ![Dick\_Visser](https://avatars.discourse-cdn.com/v4/letter/d/d9b06d/32.png) [@Dick\_Visser](https://forum.ansible.com/u/Dick_Visser)\
**Post date:** [January 11, 2022, 5:11pm UTC](https://forum.ansible.com/t/replace-indented-string-in-file/36186/6 "2022-01-11T17:11:03Z")

</div>

Ah OK.  
In such cases it is very hard to get the escaping right by hand, so what I usually do is use a helper task using regex\_escape.  
This lets ansible to the escaping for you. Once you have that escaped regex, the only thing you need to do, is to add parenthesis:
