Right regex to match -- comments or blank line?

Discussion in 'Perl' started by Dave Sisk, Mar 5, 2004.

  1. Dave Sisk

    Dave Sisk Guest

    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.
    Dave Sisk, Mar 5, 2004
    #1
    1. Advertising

  2. Dave Sisk

    jan Guest

    chomp(); is removing the \n;

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

    Cheers,
    Jan

    "Dave Sisk" <.0nospam0> wrote in message news:<0QT1c.45536$%>...
    > 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.
    jan, Mar 5, 2004
    #2
    1. Advertising

  3. "jan" <> wrote in message
    news:...
    > 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ürgen Exner, Mar 5, 2004
    #3
  4. Dave Sisk

    Jim Gibson Guest

    In article <>, jan
    <> wrote:

    [top-posting fixed]

    > "Dave Sisk" <.0nospam0> wrote in message
    > news:<0QT1c.45536$%>...
    > > 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/) {


    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*$//;

    > > $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.



    > chomp(); is removing the \n;
    >
    > try:
    > unless ($insert =~ /^--|^\s?$/)
    >
    > Cheers,
    > Jan
    >


    Note: this newsgroup is defunct. Try comp.lang.perl.misc in the future.
    Jim Gibson, Mar 5, 2004
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. hiwa
    Replies:
    0
    Views:
    631
  2. Monk
    Replies:
    10
    Views:
    1,439
    Michael Wojcik
    Apr 20, 2005
  3. Ahmet Kilic

    regex problem for comments line

    Ahmet Kilic, Sep 28, 2009, in forum: Ruby
    Replies:
    4
    Views:
    544
    whjtsppes
    Apr 12, 2012
  4. Replies:
    3
    Views:
    157
    Paul Lalli
    Oct 27, 2005
  5. Peng Yu
    Replies:
    6
    Views:
    97
    C.DeRykus
    Jun 14, 2010
Loading...

Share This Page