Right regex to match -- comments or blank line?

D

Dave Sisk

Hi Folks:

I need the right regex to match either a comment line (--) or a blank line.
What I've got below doesn't seem to be working like I'd expect. I've posted
a snippet of what I have below...would anyone be kind enough to point me in
the right direction? I also apparently need to strip the ';' off the end
of the INSERT statement...

TIA,
Dave

-----------------------------------------------------------
while(<SCRIPT>) {

chomp;

$insert = $_;

unless ($insert =~ /^--|^\s+$\n/) {

$rv = $dbh->do($insert);

}

}



The script I'm reading in looks like this:

-- Some comments on this line, the next line is a blank line, then the next
lines are inserts.



insert into sometable values('whatever1');

insert into sometable values('whatever2');

etc.
 
J

Jürgen Exner

jan said:
chomp(); is removing the \n;

Actually it is removing whatever the value of $/ is. Granted, by default
that is \n, but some people may change it and wonder why chomp() doesn't
work any more.

jue
 
J

Jim Gibson

[top-posting fixed]

Do you want to allow for blanks before the '--'?
Do you require a blank line have whitespace in it?

If "yes" and "no", change to:
unless ( $insert =~ /^\s*--|^\s*$/ ) {

To strip off semi-colon at end:

$insert =~ s/;\s*$//;
chomp(); is removing the \n;

try:
unless ($insert =~ /^--|^\s?$/)

Cheers,
Jan

Note: this newsgroup is defunct. Try comp.lang.perl.misc in the future.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,733
Messages
2,569,439
Members
44,829
Latest member
PIXThurman

Latest Threads

Top