Language: PHP
Task: Split a SQL file after each semicolon which is not in quotes.
My trials: match space outside quotes I thought would help me out, but there are some strings which fail on this regex:
Modified Regex: ;(?=[^']*'[^']+';|[^']*$) (modifiers: gm)
My string:
$string = "
-- generated at Wed, 07 Nov 2007 14:18:30 +0100
SET NAMES utf8;
-- Create Table
DROP TABLE IF EXISTS `foo`;
CREATE TABLE `foo` (
`fooID` int(10) unsigned NOT NULL auto_increment,
`barID` int(10) unsigned NOT NULL default '0',
PRIMARY KEY (`fooID`)
) ENGINE=MyISAM AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;
-- Data
INSERT INTO `foo` VALUES (';'. ';', '\';', ''';');
";
For testing smaller string variations which already fail:
$stringb = "INSERT INTO `foo` VALUES (';',';','','');";
$stringc = "INSERT INTO `foo` VALUES ('',' d ', ';');
INSERT INTO `foo` VALUES (';',' d ', ';');";
The red semicolons should not match but they do. Maybe there are other string constellations which fail too. The main task is to ignore semicolons inside quotes.
Is there a way to do this with a regex?
Thanks