简体   繁体   中英

Find and substitute semicolons which are not enclosed in quotes

I have a multiline SQL script in which I need to replace the ";" characters which are at the end of the statements with the sequence "\\nGO", leaving the ";" which are not at the end of the statements (the ones which are included in a string).

This is an example text

Insert into  TABLE1 (A, B) values ('a','b');
Insert into  TABLE2 (COMMAND) values ('/* Script generated at 20.04.2006 12:38:44 */

/* error_permissible = 955*/
Create Table map_encodekey
( privatekey VARCHAR2(30)  NOT NULL
);

/* error_permissible = 2260*/
Alter Table map_encodekey
  Add Constraint map_encodekey_pk
  Primary Key
   (privatekey);
');

So I need to replace only the two semicolons at the end of the two INSERT statements, leaving the ones in the string (wchich is a SQL command, but this doesn't matter).

I tried this, but it finds me all the ";"

/(?<!['])(?<![;])[;](?![;])(?!['])/g

What do you think the regex should look like?

;(?=(?:[^']*'[^']*')*[^']*$)

You can use this and replace by \\nGO .See demo.

https://regex101.com/r/cJ6zQ3/35

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM