Take the 2-minute tour ×

I've a string with a mysql structure dump.


-- Table structure for table `contig`

/*!40101 SET character_set_client = utf8 */;
CREATE TABLE `contig` (
  `id` int(10) NOT NULL,
  `seq` varchar(1000) DEFAULT NULL,
  PRIMARY KEY (`id`)
/*!40101 SET character_set_client = @saved_cs_c

Now I want to select each string wich contains a create table query. I tried this with re.findall and with several regexes (E.g. (CREATE TABLE)(.*)[^;]*)

But each regex I tried returned at best [('CREATE TABLE', " 'contig' (")]. But I want the whole query as one element in the list. I tried rstrip to remove break lines, but nothing worked.

How can I get each INSERT INTO query as a whole?

share|improve this question
add comment

1 Answer

up vote 4 down vote accepted

You can not select nested parentheses sequence using regular expressions. So the task you want to perform is unsolvable.

But as a hack, you can try something like this:

>>> re.findall('CREATE TABLE[^;]+;', s)
['CREATE TABLE `contig` (...) ENGINE=InnoDB DEFAULT CHARSET=latin1;']

Part (.*) is excess in your regex.

share|improve this answer
add comment

Your Answer


