# Regexp failure:  Unmatched group

**URL:** <https://forum.ansible.com/t/regexp-failure-unmatched-group/28947>\
**Category:** Ansible Project\
**Created:** [November 5, 2018, 8:56pm UTC](https://forum.ansible.com/t/regexp-failure-unmatched-group/28947 "2018-11-05T20:56:31Z")\
**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 5, 2018, 8:56pm UTC](https://forum.ansible.com/t/regexp-failure-unmatched-group/28947/1 "2018-11-05T20:56:31Z")

</div>

I am getting a failure with the latter half of my task. I replaced the working stuff with …  
According to a regexp tester, every group does have something, and I don’t have empty groups. [https://regex101.com/r/MNkwaD/1](https://regex101.com/r/MNkwaD/1)  
I changed stdout\_callback to minimal, but it didn’t look much different.  
I am so new to regexp I could use another pair of eyes. I don’t know what else to look for here.

Code:

`

- name: Sanitize vm.cfg step 2 of 2  
lineinfile:  
path: “{{ file }}”  
regexp: “{{ item.regexp }}”  
line: “{{ item.line }}”  
backrefs: “{{ item.backrefs }}”  
with\_items:  
…
- regexp: (^disk\W+[)?(‘file:[^’]+‘(,)?)?(‘phy:[^’]+’(,)?)?(])?  
line: \1\2\3\6  
backrefs: yes

`

Failure:

`  
failed: [ovmftp] (item={u’regexp’: u"(^disk\W+\[)?(‘file:[^’]+‘(,)?)?(‘phy:[^’]+’(,)?)?(])?", u’line’: u’\1\2\3\6\5\4’, u’backrefs’: True}) =\> {“changed”: false, “item”: {“backrefs”: true, “line”: “\1\2\3\6\5\4”, “regexp”: “(^disk\W+\[)?(‘file:[^’]+‘(,)?)?(‘phy:[^’]+’(,)?)?(])?”}, “module\_stderr”: “tput: No value for $TERM and no -T specified\ntput: No value for $TERM and no -T specified\nTraceback (most recent call last):\n File "", line 113, in \n File "", line 105, in \_ansiballz\_main\n File "", line 48, in invoke\_module\n File "/tmp/ansible\_lineinfile\_payload\_Wm2J\_Z/ **main**.py", line 520, in \n File "/tmp/ansible\_lineinfile\_payload\_Wm2J\_Z/ **main**.py", line 511, in main\n File "/tmp/ansible\_lineinfile\_payload\_Wm2J\_Z/ **main**.py", line 300, in present\n File "/usr/lib64/python2.7/re.py", line 266, in \_expand\n return sre\_parse.expand\_template(template, match)\n File "/usr/lib64/python2.7/sre\_parse.py", line 800, in expand\_template\n raise error, "unmatched group"\nsre\_constants.error: unmatched group\n”, “module\_stdout”: “”, “msg”: “MODULE FAILURE\nSee stdout/stderr for the exact error”, “rc”: 1}

`

---

<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 5, 2018, 9:23pm UTC](https://forum.ansible.com/t/regexp-failure-unmatched-group/28947/2 "2018-11-05T21:23:17Z")

</div>

So you have a regexp that isn't working, but haven't told us what you are trying to do, so it's actually impossible to help you.

---

<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 5, 2018, 9:28pm UTC](https://forum.ansible.com/t/regexp-failure-unmatched-group/28947/3 "2018-11-05T21:28:29Z")

</div>

> So you have a regexp that isn’t working, but haven’t told us what you are trying to do, so it’s actually impossible to help you.
> 
> –  
> Kai Stian Olstad

In the link I provided there is a sample string. I capture 6 different groups. In short, I am trying to remove all phy devices strings from the line. From my line, you can see I am trying to only retain groups 1,2,3 and 6. When complete, I should have a new “disk = ['file…]” string that only contains devices being referenced by a file instead of a physical device.

My apologies that I wasn’t more clear.

---

<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 5, 2018, 10:35pm UTC](https://forum.ansible.com/t/regexp-failure-unmatched-group/28947/4 "2018-11-05T22:35:05Z")

</div>

If you that is the only place you have phy: replace module would be a lot easier.

&nbsp;&nbsp;- replace:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;path: /path/to/file  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;regexp: (, )?'phy:[^']+'  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;replace: ''

---

<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 5, 2018, 10:42pm UTC](https://forum.ansible.com/t/regexp-failure-unmatched-group/28947/5 "2018-11-05T22:42:24Z")

</div>

> If you that is the only place you have phy: replace module would be a lot easier.
> 
> - replace:  
> path: /path/to/file  
> regexp: (, )?‘phy:[^’]+’  
> replace: ‘’
> 
> –  
> Kai Stian Olstad

Yep, you are right. That is much easier. I’ll go that route. Out of curiosity do you think it is a syntax issue with my hodgepodge of groups causing the “unmatched group” error? or a bug?

---

<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 6, 2018, 12:19pm UTC](https://forum.ansible.com/t/regexp-failure-unmatched-group/28947/6 "2018-11-06T12:19:24Z")

</div>

I don't think it's a bug, more in the line it's impossible or at least very hard to write regexp to cover that string.
