regexp help? Replace part of string

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.

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.

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

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

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.

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. :slight_smile:

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.