Dionys
Dionys

Reputation: 3113

How to remove square brackets with using multiple marking groups within each line of a text file?

I can't figure out how to use group matches for replacement.

Here is my issue: I have an sql file I want to modify in order to make it mysql compatible.

The file looks something like this:

INSERT INTO Client ([name], [id], [company]) VALUES ('Roger', 1, '[Coca-cola]');  
INSERT INTO Client ([name], [id], [company]) VALUES ('Lisa', 2, '[Nike]');

I want it to look like this:

INSERT INTO Client (name, id, company) VALUES ('Roger', 1, '[Coca-cola]');  
INSERT INTO Client (name, id, company) VALUES ('Lisa', 2, '[Nike]');

I managed to write this regex:

\\((\\[([^\\]]*)\\],? ?)+\\) VALUES

I am using sublimetext3. It is using the boost regex library.
But I don't know what to write for the replacement string.

Any ideas?

PS: The original file has several different tables with different number of rows and has 13000 lines.

Upvotes: 0

Views: 541

Answers (2)

Avinash Raj
Avinash Raj

Reputation: 174706

Regex:

(?<=\(|, )\[|\](?=,|\))

Replace the matched brackets with an empty string.

DEMO

Upvotes: 2

Mofi
Mofi

Reputation: 49097

I suggest to use the search string (?<!')\[|\](?!') and an empty string as replace string.

Explanation:

(?<!') ... a negative lookbehind for character '.

\[ ... literal character [.

| ... OR expression.

\] ... literal character ].

(?!') ... a negative lookahead expression for character '.

Find an opening square bracket not preceded by a single quote OR a closing square bracket with next character not being a single quote.

Upvotes: 2

Related Questions