Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

Re: Substitutions Within Substitutions

by Sprad (Hermit)
on May 02, 2006 at 22:18 UTC ( [id://547005]=note: print w/replies, xml ) Need Help??


in reply to Substitutions Within Substitutions

I think I figured it out:
$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 c +reate table variables), save as $3 \s* # Any spaces \w+ # Variable name \s+ # At least one space \w+ # Variable type ( # Optional (nnn) for character str +ing 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 m +ay be the last element) \s* # Any spaces )+ # Match this whole section at leas +t once ) (\s*\)\s*) # Closing paren for create table b +lock 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;
I changed the regex to grab the whole block of table elements (getting rid of some unnecessary parens in the process), then just did an easy split and join on them inside the right hand block. Then I switched the prints to a concatenation so it would actually do a substitution.

---
A fair fight is a sign of poor planning.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others having an uproarious good time at the Monastery: (3)
As of 2024-04-25 17:37 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found