Beefy Boxes and Bandwidth Generously Provided by pair Networks Cowboy Neal with Hat
Don't ask to ask, just ask
 
PerlMonks  

Re: Regexp in mysqldump

by clueless newbie (Friar)
on Apr 16, 2010 at 12:47 UTC ( #835089=note: print w/ replies, xml ) Need Help??


in reply to Regexp in mysqldump

May I offer

#! use Smart::Comments; use strict; use warnings; my $sql=<<"__SQL__"; /* comment! */ create table `table name`( # another comment! `field name1` type1 not null default 'x has a blank or two', `field name2` type2 default null, # ... primary key (`pk name`), key `key name` (`field name1`, `field name2`) ) engine=other things here; __SQL__ ### $sql my $RegExp_s=qr{(?: (`(?:[^`]|\`)*?`) # backticked string |(?:\/\*.*?\*\/) # /* */ comments |(?:'(?:[^']|\')*?') # single quoted strings |(?:"(?:[^"]|\")*?") # qouble quoted strings |(?m:\#.+?$) # single line # trailing comments )}sx; #' # . includes \n while ($sql =~ m{$RegExp_s}g) { if (defined $-[1]) { # backticked string substr($sql,$-[1],$+[1]-$-[1])=~ s/ /_/g; }; }; ### $sql __END__
which yields
### $sql: '/* comment! */ create table `table name`( # another comment! `field name1` type1 not null default \'x has a blank or two\', `field name2` type2 default null, # ... primary key (`pk name`), key `key name` (`field name1`, `field name2`) ) engine=other things here; ' ### $sql: '/* comment! */ create table `table_name`( # another comment! `field_name1` type1 not null default \'x has a blank or two\', `field_name2` type2 default null, # ... primary key (`pk_name`), key `key_name` (`field_name1`, `field_name2`) ) engine=other things here; '
Commented the regex.


Comment on Re: Regexp in mysqldump
Select or Download Code
Re^2: Regexp in mysqldump
by clueless newbie (Friar) on Apr 16, 2010 at 16:02 UTC
    Ignore the above! Try the following instead
    #! use Smart::Comments; use strict; use warnings; my $sql=<<"__SQL__"; /* comment! */ create table `table name`( # another comment! `field name1` type1 not null default 'x has a blank or two', `field name2` type2 default null, # ... primary key (`pk name`), key `key name` (`field name1`, `field name2`) ) engine=other things here; __SQL__ my $RegExp_s=qr{( (`(?:[^`]|\`)*?`) # backticked string |(?:\/\*.*?\*\/) # /* */ comments |(?:'(?:[^']|\')*?') # single quoted strings |(?:"(?:[^"]|\")*?") # qouble quoted strings |(?m:\#.+?$) # single line # trailing comments )}sx; #' # . includes \n ### $sql $sql =~ s{$RegExp_s}{defined $2 ? TreatBackTickedString($2) : $1}eg; ### $sql sub TreatBackTickedString { # Jest Fur Phun: return substr($_[0],0,-1).substr($_[0],1); }; __END__

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://835089]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others studying the Monastery: (7)
As of 2014-04-16 11:03 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    April first is:







    Results (424 votes), past polls