$sql =~ s{ (create\s+table\s+\w+\s*\() # "create table foo (", save as $1 ( # Save all the elements as $2 ( # Grab this pattern (each of the create table variables), 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 [\w\s]* # Any words and/or spaces ("null" or "not null", specifically) ,? # Optional comma (optional as it may be the last element) \s* # Any spaces )+ # Match this whole section at least once ) (\s*\)\s*) # Closing paren for create table block with surrounding whitespace, save as $5 } { @elements = split(/,/, $2); foreach $elem (@elements) { if ($elem =~ /null\s*$/i) { next; } $elem .= " NOT NULL"; } $1 . (join ",", @elements) . $5 }gex;