# regexp help?  Replace part of string

**URL:** <https://forum.ansible.com/t/regexp-help-replace-part-of-string/28923>\
**Category:** Ansible Project\
**Created:** [November 1, 2018, 10:58pm UTC](https://forum.ansible.com/t/regexp-help-replace-part-of-string/28923 "2018-11-01T22:58:59Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![John\_Harmon](https://avatars.discourse-cdn.com/v4/letter/j/edb3f5/32.png) [@John\_Harmon](https://forum.ansible.com/u/John_Harmon)\
**Post date:** [November 1, 2018, 10:58pm UTC](https://forum.ansible.com/t/regexp-help-replace-part-of-string/28923/1 "2018-11-01T22:58:59Z")

</div>

I have been thinking about doing this with lineinfile with backrefs, or with the replace module, but I am at a loss as to where to start (I am horrible with regexp…)

Consider the following:

`vif = ['mac=00:21:f6:A1:B7:49,bridge=1053d3f53c']`

I would like to replace the last three octets, but leave everything else alone:  
ie

`vif = ['mac=00:21:f6: **AA:BB:CC** ,bridge=1053d3f53c']`

I am using a regexp tester, but am not getting the results I would like. Is regexp the right way to tackle this? Is there another/better way? If the former, I could use some regexp help. Thanks in advance.

---

<div class="post-metadata">

**Author:** ![Brad2](https://avatars.discourse-cdn.com/v4/letter/b/49beb7/32.png) [@Brad2](https://forum.ansible.com/u/Brad2)\
**Post date:** [November 2, 2018, 11:27am UTC](https://forum.ansible.com/t/regexp-help-replace-part-of-string/28923/2 "2018-11-02T11:27:20Z")

</div>

Well, not ansible, but you should be able to use as a reference. This works with sed:  
`sed -e"s|^\(vif=\['mac=..:..:..:\)..:..:..\(,bridge=.*'\]\)$|\1AA:BB:CC\2|" file`  
where file has your text.

---

<div class="post-metadata">

**Author:** ![Kai\_Stian\_Olstad](https://avatars.discourse-cdn.com/v4/letter/k/e19adc/32.png) [@Kai\_Stian\_Olstad](https://forum.ansible.com/u/Kai_Stian_Olstad)\
**Post date:** [November 2, 2018, 4:48pm UTC](https://forum.ansible.com/t/regexp-help-replace-part-of-string/28923/3 "2018-11-02T16:48:17Z")

</div>

Brad gave you a sed way, with lineinfile it would look something like this

&nbsp;&nbsp;- lineinfile:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;path: /path/to/file  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;backrefs: yes  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;regexp: ^(vif = \['mac=)((\w{2}:){3})((\w{2}:?){3})(.+)$  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;line: \1\2AA:BB:CC\6

---

<div class="post-metadata">

**Author:** ![John\_Harmon](https://avatars.discourse-cdn.com/v4/letter/j/edb3f5/32.png) [@John\_Harmon](https://forum.ansible.com/u/John_Harmon)\
**Post date:** [November 2, 2018, 4:52pm UTC](https://forum.ansible.com/t/regexp-help-replace-part-of-string/28923/4 "2018-11-02T16:52:36Z")

</div>

Awesome, thanks guys! Kai, I am unsure if I would have got that one my own. I can see what it is doing now, but is near voodoo to me. I thought I understood backrefs at a very basic level, but this proves I have a long way to go. Thank you.

---

<div class="post-metadata">

**Author:** ![Brad2](https://avatars.discourse-cdn.com/v4/letter/b/49beb7/32.png) [@Brad2](https://forum.ansible.com/u/Brad2)\
**Post date:** [November 2, 2018, 8:39pm UTC](https://forum.ansible.com/t/regexp-help-replace-part-of-string/28923/5 "2018-11-02T20:39:40Z")

</div>

I knew it wasn’t ansible, but just didn’t have quick access to test it. So, gave sed so that he could see a working regex. 🙂

---

<div class="post-metadata">

**Author:** ![John\_Harmon](https://avatars.discourse-cdn.com/v4/letter/j/edb3f5/32.png) [@John\_Harmon](https://forum.ansible.com/u/John_Harmon)\
**Post date:** [November 2, 2018, 8:53pm UTC](https://forum.ansible.com/t/regexp-help-replace-part-of-string/28923/6 "2018-11-02T20:53:08Z")

</div>

No worries. Chances are I or someone else will need to reference the sed portion of this in the future. The more information the merrier.
