# Need help with regex\_replace

**URL:** <https://forum.ansible.com/t/need-help-with-regex-replace/4379>\
**Category:** Get Help\
**Created:** [March 14, 2024, 9:28am UTC](https://forum.ansible.com/t/need-help-with-regex-replace/4379 "2024-03-14T09:28:25Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![hieronymus](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/hieronymus/32/1624_2.png) [@hieronymus](https://forum.ansible.com/u/hieronymus)\
**Post date:** [March 14, 2024, 9:28am UTC](https://forum.ansible.com/t/need-help-with-regex-replace/4379/1 "2024-03-14T09:28:25Z")

</div>

Hi guys,

I’ve a problem with **regex\_replace** module. It seems that my expression is correct but I’m not getting the expected result.

So I’m using a raw command and use register to get the result.  
Here’s my debug:

```auto
- name: Get virtual info
  ansible.builtin.debug:
    msg: " vminfo : {{ vminfo.stdout_lines }}"

```

Here’s the result:

```auto
ok: [vm01-vm-dev01.local.com] =>
  msg: ' vminfo : [''1 vm01-vm-dev01'']'

```

I need to retrieve the second part of this string : `vm01-vm-dev01` and here is my code:

```auto
- name Get virtual info
  ansible.builtin.debug:
    msg: " vminfo : {{ vminfo.stdout_lines | regex_replace('^([0-9])\\s(vm.*)$', '\\2') }}"

```

With **regex\_replace** I tried to isolate the elements separated by a space and retrieve the second one but unfortunately nothing happens!

As anyone an idea what’s wrong by my regex? Any ideas?

Regards,

---

<div class="post-metadata">

**Author:** ![chris](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/chris/32/293_2.png) [@chris](https://forum.ansible.com/u/chris)\
**Post date:** [March 14, 2024, 9:38am UTC](https://forum.ansible.com/t/need-help-with-regex-replace/4379/2 "2024-03-14T09:38:40Z")

</div>

If you only want the first item in the list you could use `vminfo.stdout_lines[0]` and then [split](https://docs.ansible.com/ansible/latest/collections/ansible/builtin/split_filter.html) to get the second part of the string, for example something like this might work (untested):

```yaml
- name: Set a fact for the VM name
  ansible.builtin.set_fact:
    vmname: "{{ (vminfo.stdout_lines[0] | ansible.builtin.split)[1] }}"

```

---

<div class="post-metadata">

**Author:** ![Dustin](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/dustin/32/2573_2.png) [@Dustin](https://forum.ansible.com/u/Dustin)\
**Post date:** [March 14, 2024, 12:48pm UTC](https://forum.ansible.com/t/need-help-with-regex-replace/4379/3 "2024-03-14T12:48:19Z")

</div>

Notice the “[” and "]" surrounding the string you want. That means that the variable `vminfo.stdout_lines` is a list, and it contains a single string. That means that you are passing a list to regex\_replace instead of a string.

```bash
ok: [vm01-vm-dev01.local.com] =>
  msg: ' vminfo : [''1 vm01-vm-dev01'']'

```

As @chris shows in his example, you need to identify which item in `stdout_lines` that you want to pass to `regex_replace`.

Edit: Corrected vminfo reference to vminfo.stdout\_lines

---

<div class="post-metadata">

**Author:** ![hieronymus](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/hieronymus/32/1624_2.png) [@hieronymus](https://forum.ansible.com/u/hieronymus)\
**Post date:** [March 15, 2024, 2:13pm UTC](https://forum.ansible.com/t/need-help-with-regex-replace/4379/4 "2024-03-15T14:13:30Z")

</div>

Thanks a lot for your answer.  
It’s now clear and I have now been able to solve my problem.

---

<div class="post-metadata">

**Author:** ![Dustin](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/dustin/32/2573_2.png) [@Dustin](https://forum.ansible.com/u/Dustin)\
**Post date:** [March 15, 2024, 3:06pm UTC](https://forum.ansible.com/t/need-help-with-regex-replace/4379/5 "2024-03-15T15:06:48Z")

</div>

Please, don’t forget to mark @chris message as the solution so others can more easily find the answer to similar issue.  
🙂

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/ansible/original/1X/1882d9a01da5d8996a862359b0913cc568a39b84.svg) [@system](https://forum.ansible.com/u/system)\
**Post date:** [October 18, 2024, 6:31am UTC](https://forum.ansible.com/t/need-help-with-regex-replace/4379/6 "2024-10-18T06:31:29Z")

</div>

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.
