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
Post a Comment