Sprad has asked for the wisdom of the Perl Monks concerning the following question:
In this case, I'd want to add the string "NOT NULL" to the end of variables 1, 4, and 6, and leave the others alone. I've entered the example in a nicely indented fashion, but in practice, I have no control over the formatting. There might be any amount or type of whitespace wherever SQL allows it. Also, there might be any number of "create table" blocks in a given file.create table foo ( somevar1 sometype, somevar2 sometype null, somevar3 sometype not null, somevar4 sometype(20), somevar5 sometype(20) null, somevar6 sometype(10))
I've got a pattern that gets me most of the way there, but it's only hitting the last element in each create table list. How do I do this correctly?
$str =~ s{ (create\s+table\s+\w+\s*\() # "create table foo (", save as $1 ( # Grab this pattern (each of the crea +te table variables), save as $2 ( # Save as $3 \s* # Any spaces \w+ # Variable name \s+ # At least one space \w+ # Variable type ( # Optional (nnn) for character string + types, save as $4 \( # Literal open paren \d+ # Any number of digits \) # Literal close paren )? # The ? makes it optional \s* # Any spaces ) # "varname vartype " (not the null/no +t null part) ( # Save as $5 [\w\s]* # Any words and/or spaces ("null" or +"not null", specifically) ) ( # Save as $6 ,? # Optional comma (optional as it may +be the last element) \s* # Any spaces ) )+ # Match this whole section at least o +nce (\s*\)\s*) # Closing paren for create table bloc +k with surrounding whitespace, save as $7 } { print $1; print $3; if ($5 =~ /null/i) { print $5; } else { print " NOT NULL"; } print $6; print $7; }gex; # Output: create table foo (somevar6 sometype(10) NOT NULL)
---
A fair fight is a sign of poor planning.
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: Substitutions Within Substitutions
by BrowserUk (Patriarch) on May 02, 2006 at 22:43 UTC | |
Re: Substitutions Within Substitutions
by Anonymous Monk on May 02, 2006 at 22:48 UTC | |
Re: Substitutions Within Substitutions
by davidrw (Prior) on May 02, 2006 at 22:00 UTC | |
Re: Substitutions Within Substitutions
by Sprad (Hermit) on May 02, 2006 at 22:18 UTC | |
Re: Substitutions Within Substitutions
by Fletch (Bishop) on May 03, 2006 at 02:02 UTC |