# Help with regex, replace, and quotes

**URL:** <https://forum.ansible.com/t/help-with-regex-replace-and-quotes/15198>\
**Category:** Ansible Project\
**Tags:** ubuntu\
**Created:** [May 8, 2014, 7:56pm UTC](https://forum.ansible.com/t/help-with-regex-replace-and-quotes/15198 "2014-05-08T19:56:04Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![David\_Reagan](https://avatars.discourse-cdn.com/v4/letter/d/71e660/32.png) [@David\_Reagan](https://forum.ansible.com/u/David_Reagan)\
**Post date:** [May 8, 2014, 7:56pm UTC](https://forum.ansible.com/t/help-with-regex-replace-and-quotes/15198/1 "2014-05-08T19:56:04Z")

</div>

I’ve been trying to use replace to replace config lines in a config file that I have good reasons not to template.

Specifically, it’s php associative array. Thus, I need to be able to match lines that contain quotes.

So far, I can’t get it to work.

I eventually pulled the essentials out of my role and into a single playbook that I’ve posted on pastebin [http://pastebin.com/Y6X5dZqz](http://pastebin.com/Y6X5dZqz)

So, if a file contains

> $CONFIG = array (  
> ‘dbhost’ =\> ‘laskdjflkjsdlfkj’,  
> );

How do I replace ‘dbhost’ =\> ‘whatever the setting happens to be.’, with whatever I want? Also, the single quotes can sometimes be double quotes.

I generally get an error that looks something like:

> fatal: [192.168.88.2] =\> failed to parse: SUDO-SUCCESS-wlskunaawgbgwiokatisjtavicfvfbux  
> Traceback (most recent call last):  
> File “/home/vagrant/.ansible/tmp/ansible-tmp-1399578173.9-133172102011107/replace”, line 1330, in   
> main()  
> File “/home/vagrant/.ansible/tmp/ansible-tmp-1399578173.9-133172102011107/replace”, line 123, in main  
> supports\_check\_mode=True  
> File “/home/vagrant/.ansible/tmp/ansible-tmp-1399578173.9-133172102011107/replace”, line 350, in **init**  
> (self.params, self.args) = self.\_load\_params()  
> File “/home/vagrant/.ansible/tmp/ansible-tmp-1399578173.9-133172102011107/replace”, line 901, in \_load\_params  
> items = shlex.split(args)  
> File “/usr/lib/python2.7/shlex.py”, line 279, in split  
> return list(lex)  
> File “/usr/lib/python2.7/shlex.py”, line 269, in next  
> token = self.get\_token()  
> File “/usr/lib/python2.7/shlex.py”, line 96, in get\_token  
> raw = self.read\_token()  
> File “/usr/lib/python2.7/shlex.py”, line 172, in read\_token  
> raise ValueError, “No closing quotation”  
> ValueError: No closing quotation

My local machine is running Ubuntu 14.04, and Ansible devel branch from a couple days ago.

My testing vagrant vm is Ubuntu 12.04.

Thanks!

---

<div class="post-metadata">

**Author:** ![James\_Cammarata](https://avatars.discourse-cdn.com/v4/letter/j/7c8e57/32.png) [@James\_Cammarata](https://forum.ansible.com/u/James_Cammarata)\
**Post date:** [May 9, 2014, 5:52am UTC](https://forum.ansible.com/t/help-with-regex-replace-and-quotes/15198/2 "2014-05-09T05:52:42Z")

</div>

This worked for me:

> > > re.compile(r"“”(?:‘dbhost’)\s\*(?:=\>)\s\*'"['“],”“”).match(r"“”‘dbhost’ =\> ‘laskdjflkjsdlfkj’,“”").groups()[0]  
> > > ‘laskdjflkjsdlfkj’

It would also match a string with unbalanced quotes like 'foo" but it’s a much longer regex to enforce avoid that (at least in my experience, others may have better regex knowledge).
