0

I have an sql dump of different tables each with different amount of fields, I want to insert a query after each one, so I'm trying to find a regex statment that would retreive:

CREATE TABLE  cms_audit (
aud_id bigint NOT NULL IDENTITY(1,1),
  user_id int DEFAULT NULL,
  client_id int NOT NULL,
  aud_event varchar(500) NOT NULL,
  aud_type varchar(150) NOT NULL,
  aud_string varchar(1000) DEFAULT NULL,
  aud_date datetime DEFAULT NULL
) 
-- --------------------------------------------------------

My regex is CREATE TABLE .*-- (in notepad++ I've checked the box that say's . matches newline) in my head this means get all that starts with "create table" and whatever is after it until you reach "--". However this statement is retrieving the entire file instead of getting each "create table" query separately, what am I missing?.

I have also tried CREATE TABLE (.*|\n)*--.. didn't work.

1 Answer 1

1

You need to use a regex with any character except --. To achieve this you can do:

CREATE TABLE (?:(?!--).)*

EDIT

The ?! is to make a Negative Lookahead of the string --. Nothing with this string will match this expression.

You can see and test it with this link (it's very well explained and a good tool):

https://regex101.com/r/mR9fD4/1

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

3 Comments

That works! Thanks. Why do I have to use the ?! though? can you explain it to me please?
Thanks. Why wouldn't this work though? CREATE TABLE (?:(?!--).*)
Because * repeats only the last character (in this case anything as you put a .) although you'd put (?!--). So this, match all the string. You should put the * after all the group to say that must match anything (.) expect -- any times (*). I hope it is clearer ;)

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.