regex - Why does this substitution also remove a whitespace character? -


i writing script extract , convert sql statements file. need convert sql unloaded froma gupta sqlbase database sql sqlserver can understand.

one task replace keywords not allowed column names compatible name.

in following code $commands array ref contains sql statements. (there more code here, extracted because shouldn't relevant here)

my @keywords = ("left", "right", "percent", "file", "print", "cross", "plan", "top", "end", "file", "default", "check", "text");  foreach $cmd (@$commands) {      foreach $kw (@keywords) {         $cmd =~ s/\b$kw\b[^(]/_$kw/gi;     }       push @$converted, $cmd;  } 

this works fine statements in following command "default" gets replaced "_default instead of "_default". second quotation mark lost.

  create table sysadm.subtype ( id_subtype integer not null,    id_type integer not null,    type varchar(1),    bezeich varchar(60),    num_color integer,    num_txtcolor integer,    "default" smallint,    gener_arba smallint,    projektplanung smallint) 

is there way modify regular expression/substition not remove second quotation mark? or other way?

[^(] matches single character not left opening paranthesis.

you want use negative zero-width lookahead assertion instead:

s/\b$kw\b(?!\()/_$kw/gi; 

(alternatively: (?![(]))

you can add replaced character string:

s/\b$kw\b([^(])/_$kw$1/gi; 

but note not work in cases. if there nothing after keyword, pattern not match whereas zero-width assertion will.


Comments

Popular posts from this blog

image - ClassNotFoundException when add a prebuilt apk into system.img in android -

I need to import mysql 5.1 to 5.5? -

Java, Hibernate, MySQL - store UTC date-time -