Right regex to match -- comments or blank line?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Dave Sisk

    Right regex to match -- comments or blank line?

    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('whateve r1');

    insert into sometable values('whateve r2');

    etc.




  • jan

    #2
    Re: Right regex to match -- comments or blank line?

    chomp(); is removing the \n;

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

    Cheers,
    Jan

    "Dave Sisk" <dsisk@nc.rr.co m.0nospam0> wrote in message news:<0QT1c.455 36$%d3.4130346@ twister.southea st.rr.com>...[color=blue]
    > 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('whateve r1');
    >
    > insert into sometable values('whateve r2');
    >
    > etc.[/color]

    Comment

    • Jürgen Exner

      #3
      Re: Right regex to match -- comments or blank line?

      "jan" <jan@ossifrage. net> wrote in message
      news:fcfd857f.0 403050735.7d2e6 0e6@posting.goo gle.com...[color=blue]
      > chomp(); is removing the \n;[/color]

      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


      Comment

      • Jim Gibson

        #4
        Re: Right regex to match -- comments or blank line?

        In article <fcfd857f.04030 50735.7d2e60e6@ posting.google. com>, jan
        <jan@ossifrage. net> wrote:

        [top-posting fixed]
        [color=blue]
        > "Dave Sisk" <dsisk@nc.rr.co m.0nospam0> wrote in message
        > news:<0QT1c.455 36$%d3.4130346@ twister.southea st.rr.com>...[color=green]
        > > 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/) {[/color][/color]

        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*$//;
        [color=blue][color=green]
        > > $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('whateve r1');
        > >
        > > insert into sometable values('whateve r2');
        > >
        > > etc.[/color][/color]

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

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

        Comment

        Working...