Anyone who's up to giving regex advice, please, read on. I'm a little shaky on the subject, and would appreciate some good tips.

At work, I am currently working with a number of PL/SQL files. We have a test database and a QA database, and I often need to switch my scripts from referencing one to the other. As PL/SQL doesn't seem to have a pleasant way of doing this, I'm hoping I can surround the database names with identifiers within C-style comments so that the SQL procedures will compile properly, and at the same time Perl can identify the database names and replace them.

As an example, I'd like to be able to write

SELECT * FROM MY_SCHEMA.MY_TABLE@/*<DATABASE>*/MY_DB/*</DATABASE>*/ MY, YOUR_SCHEMA.YOUR_TABLE@/*<DATABASE>*/YOUR_DB/*</DATABASE>*/ YOUR WHERE MY.JOIN_KEY = YOUR.JOIN_KEY

in my PL/SQL scripts, and have Perl be able to identify the database names.

Sound simple? Well, it seems to be...

To test how easy it was to extract the database names in this way, I made a small test script.

#! /usr/bin/perl -w use strict ; use warnings ; use diagnostics ; use Data::Dumper ; $|++ ; #--------------------------------------- print "1ST TRY\n------\n" ; my @db_list_one = () ; my $text_one = q( SELECT * FROM SYNERGEN.SA_ASSET@/*<DATABASE>*/SGENQA/*</DATABASE>*/ ) ; my $matches_one = 0 ; if ( $text_one =~ m/\/\*\s*<DATABASE>\s*\*\/(.*)\/\*\s*<\/DATABASE>\s*\*\//i ) { push( @db_list_one, $1 ) ; $matches_one++ ; } print Dumper( \@db_list_one ), "\n\n" ; #--------------------------------------- print "2ND TRY\n------\n" ; my @db_list_two = () ; my $text_two = q( SELECT * FROM SYNERGEN.SA_ASSET@/*<DATABASE>*/SGENQA/*</DATABASE>*/, SYNERGEN.SA_WORK_ORDER@/*<DATABASE>*/SGENTEST/*</DATABASE>*/ ) ; my $text_two_copy = $text_two ; my $matches_two = 0 ; while ( $text_two_copy =~ s/\/\*\s*<DATABASE>\s*\*\/(.*)\/\*\s*<\/DATABASE>\s*\*\///i ) { push( @db_list_two, $1 ) ; $matches_two++ ; } print Dumper( \@db_list_two ), "\n\n" ;

This gives me the following output:

1ST TRY ------ $VAR1 = [ 'SGENQA' ]; 2ND TRY ------ $VAR1 = [ 'SGENQA', 'SGENTEST' ];

which was exactly what I had hoped for. After working this all out, I'm left with two big questions about my code.

Thanks for reading. :-)


_______________
D a m n D i r t y A p e
Home Node | Email

In reply to In search of regex advice by DamnDirtyApe

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.