# Deleting single or double quotation marks in a string using REGEX

**URL:** <https://forum.ansible.com/t/deleting-single-or-double-quotation-marks-in-a-string-using-regex/2628>\
**Category:** Get Help\
**Tags:** playbook\
**Created:** [November 29, 2023, 8:22am UTC](https://forum.ansible.com/t/deleting-single-or-double-quotation-marks-in-a-string-using-regex/2628 "2023-11-29T08:22:03Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Mojtaba](https://avatars.discourse-cdn.com/v4/letter/m/e56c9b/32.png) [@Mojtaba](https://forum.ansible.com/u/Mojtaba)\
**Post date:** [November 29, 2023, 8:22am UTC](https://forum.ansible.com/t/deleting-single-or-double-quotation-marks-in-a-string-using-regex/2628/1 "2023-11-29T08:22:03Z")

</div>

Hello,

Here is my playbook.

```auto
- name: Remove Quotes from Variable
  hosts: localhost
  gather_facts: false
  vars:
    original_text: "this is \"Hello'World\""

  tasks:
    - name: Display Original Text
      debug:
        var: original_text

    - name: Remove Quotes and Update Variable
      set_fact:
        original_text: "{{ original_text | regex_replace('[\"]', '') }}"

    - name: Display Updated Text
      debug:
        var: original_text

```

The task is to remove all existing single or double quotation marks in a string.

The problem is that with the current regex, only the double quotation marks in the string are deleted.

However, if I change the regex in this way `regex_replace('[\'\"]', '')`, I receive an error message. Therefore, my question is: What regex is correct for this task?

---

<div class="post-metadata">

**Author:** ![jrglynn2](https://avatars.discourse-cdn.com/v4/letter/j/94ad74/32.png) [@jrglynn2](https://forum.ansible.com/u/jrglynn2)\
**Post date:** [November 29, 2023, 4:20pm UTC](https://forum.ansible.com/t/deleting-single-or-double-quotation-marks-in-a-string-using-regex/2628/2 "2023-11-29T16:20:03Z")

</div>

This is one of those “regex is not intuitive” things.

First off, I don’t think you need to use `regex_replace`, you can just use `replace`.

But either way, you need to put the single quote inside double quotes, and escape the double quotes. I know, why? I don’t know LOL.

So the updated line that would include both replaces - I am showing both `regex_replace` and `replace`, same syntax for both:

```auto
original_text: "{{ original_text | regex_replace('\"', '') | regex_replace(\"'\", '') }}"
original_text: "{{ original_text | replace('\"', '') | replace(\"'\", '') }}"

```

---

<div class="post-metadata">

**Author:** ![shertel](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/shertel/32/1241_2.png) [@shertel](https://forum.ansible.com/u/shertel)\
**Post date:** [November 29, 2023, 5:04pm UTC](https://forum.ansible.com/t/deleting-single-or-double-quotation-marks-in-a-string-using-regex/2628/3 "2023-11-29T17:04:49Z")

</div>

The right way to escape single-quotes in single-quoted YAML strings is to double them, `regex_replace('[''\"]', '')`

But this won’t fix it inline because Jinja2 rendering removes the single quotes:

```auto
>>> from jinja2.nativetypes import NativeEnvironment
>>> e = NativeEnvironment()
>>> t = e.from_string('[''"]')
>>> t.render()
'["]'

```

So this would be a time where I simplify with a task var.

```auto
    - name: Remove Quotes and Update Variable
      set_fact:
        original_text: "{{ original_text | regex_replace(re_match, '') }}"
      vars:
        re_match: >-
          ['"]
        # or
        # re_match: '[''"]'
        # re_match: "['\"]"

```

---

<div class="post-metadata">

**Author:** ![Mojtaba](https://avatars.discourse-cdn.com/v4/letter/m/e56c9b/32.png) [@Mojtaba](https://forum.ansible.com/u/Mojtaba)\
**Post date:** [November 30, 2023, 8:44am UTC](https://forum.ansible.com/t/deleting-single-or-double-quotation-marks-in-a-string-using-regex/2628/4 "2023-11-30T08:44:46Z")

</div>

Hi,

thanks for your answer, I’ve tested your code and it works as expected.

---

<div class="post-metadata">

**Author:** ![Mojtaba](https://avatars.discourse-cdn.com/v4/letter/m/e56c9b/32.png) [@Mojtaba](https://forum.ansible.com/u/Mojtaba)\
**Post date:** [November 30, 2023, 8:46am UTC](https://forum.ansible.com/t/deleting-single-or-double-quotation-marks-in-a-string-using-regex/2628/5 "2023-11-30T08:46:28Z")

</div>

Hi,  
thanks for your answer,  
I didn’t know this approch but  
I’ve tested your code and it works as expected.

---

<div class="post-metadata">

**Author:** ![jackal\_monkey](https://avatars.discourse-cdn.com/v4/letter/j/a9adbd/32.png) [@jackal\_monkey](https://forum.ansible.com/u/jackal_monkey)\
**Post date:** [August 20, 2026, 1:55pm UTC](https://forum.ansible.com/t/deleting-single-or-double-quotation-marks-in-a-string-using-regex/2628/6 "2026-08-20T13:55:27Z")

</div>

Thank you. This helped me a lot.
