2

I've a string with a mysql structure dump.

...
/*!40111 SET @OLD_SQL_NOTES=@@SQL_NOTES, SQL_NOTES=0 */;

--
-- Table structure for table `contig`
--

DROP TABLE IF EXISTS `contig`;
/*!40101 SET @saved_cs_client     = @@character_set_client */;
/*!40101 SET character_set_client = utf8 */;
CREATE TABLE `contig` (
  `id` int(10) NOT NULL,
  `seq` varchar(1000) DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
/*!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?

0

1 Answer 1

4

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.