# Reg exp to edit a query

**URL:** <https://forum.kirupa.com/t/reg-exp-to-edit-a-query/285895>\
**Category:** Uncategorized\
**Created:** [April 7, 2009, 5:01pm UTC](https://forum.kirupa.com/t/reg-exp-to-edit-a-query/285895 "2009-04-07T17:01:33Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![imagined](https://avatars.discourse-cdn.com/v4/letter/i/a87d85/32.png) [@imagined](https://forum.kirupa.com/u/imagined)\
**Post date:** [April 7, 2009, 5:01pm UTC](https://forum.kirupa.com/t/reg-exp-to-edit-a-query/285895/1 "2009-04-07T17:01:33Z")

</div>

I have a list of dynamically generated queries, for example:

INSERT INTO table1 SET firstname = “Patty”, lastname = “Hewes”, id = $last\_inserted\_id  
INSERT INTO table1 SET phone = “5554441122” where id = $last\_inserted\_id  
INSERT INTO table1 SET address = “1200 Freedom Avenue” where id = $last\_inserted\_id

There is always different table names, but I need to be able to extract the table name from the query. I need a regular expression that will take anything between INSERT INTO and SET.

I will have an array with the queries. Use a foreach loop to analyze query by query. and if there is a repeated table name, then change the query from an INSERT query to an UPDATE query. Will end up with this:

INSERT INTO table1 SET firstname = “Patty”, lastname = “Hewes”, id = $last\_inserted\_id  
UPDATE table1 SET table1 SET phone = “5554441122” where id = $last\_inserted\_id  
UPDATE table1 SET address = “1200 Freedom Avenue” where id = $last\_inserted\_id

Thanks in advance,

Leo :hr:
