Regular expression to match any line that DOESN'T begin with a particular string

Discussion in 'Perl Misc' started by weyus@att.net, Mar 23, 2006.

  1. Guest

    I would like to match lines that do NOT being with "http". Here is my
    regex:

    /^[^(http)]/

    I am using a character class with negation and then counting on the
    grouping to force negation of the entire "http" string.

    Then of course, the first ^ anchors to the beginning of the string.

    Is this correct?

    Thanks,
    Wes
    , Mar 23, 2006
    #1
    1. Advertising

  2. wrote in news:1143074916.738167.82330
    @t31g2000cwb.googlegroups.com:

    > I would like to match lines that do NOT being with "http". Here is my
    > regex:
    >
    > /^[^(http)]/
    >
    > I am using a character class with negation and then counting on the
    > grouping to force negation of the entire "http" string.
    >
    > Then of course, the first ^ anchors to the beginning of the string.
    >
    > Is this correct?


    Of course not. You could have figured that out quite trivially yourself.

    #!/usr/bin/perl

    use strict;
    use warnings;

    while ( <DATA> ) {
    print if /^[^(http)]/;
    }

    __DATA__
    htpt

    Compare that to

    #!/usr/bin/perl

    use strict;
    use warnings;

    while ( <DATA> ) {
    print unless /^http/;
    }

    __DATA__
    htpt

    Sinan


    --
    A. Sinan Unur <>
    (remove .invalid and reverse each component for email address)

    comp.lang.perl.misc guidelines on the WWW:
    http://mail.augustmail.com/~tadmc/clpmisc/clpmisc_guidelines.html
    A. Sinan Unur, Mar 23, 2006
    #2
    1. Advertising

  3. Reeees Guest

    A. Sinan Unur <> trolled:
    > wrote in news:1143074916.738167.82330
    > @t31g2000cwb.googlegroups.com:


    > > I would like to match lines that do NOT being with "http". Here is my
    > > regex:
    > >
    > > /^[^(http)]/
    > >
    > > I am using a character class with negation and then counting on the
    > > grouping to force negation of the entire "http" string.
    > >
    > > Then of course, the first ^ anchors to the beginning of the string.
    > >
    > > Is this correct?


    > Of course not. You could have figured that out quite trivially yourself.


    What is the purpose of this statement? How does this statement help
    the user with his problem?

    Some of you seem to think that it is ok to insult posters simply
    because you may, or may not, be giving them accurate technical
    advice. But it is not ok to insult people.

    Please grow up. Or stop posting.

    cordially, as always,

    rm
    Reeees, Mar 23, 2006
    #3
  4. Re: Regular expression to match any line that DOESN'T begin witha particular string

    wrote:
    > I would like to match lines that do NOT being with "http". Here is my
    > regex:
    >
    > /^[^(http)]/
    >
    > I am using a character class with negation and then counting on the
    > grouping to force negation of the entire "http" string.
    >
    > Then of course, the first ^ anchors to the beginning of the string.
    >
    > Is this correct?


    No, a character class is just a list of characters not a pattern so [^(http)]
    is the same as [^()hpt]. You want a negative look-ahead assertion:

    /(?!^http)/


    John
    --
    use Perl;
    program
    fulfillment
    John W. Krahn, Mar 23, 2006
    #4
  5. <> wrote:
    > I would like to match lines that do NOT being with "http". Here is my
    > regex:
    >
    > /^[^(http)]/
    >
    > I am using a character class with negation and then counting on the
    > grouping to force negation of the entire "http" string.
    >
    > Then of course, the first ^ anchors to the beginning of the string.
    >
    > Is this correct?



    What happened when you tried it?


    --
    Tad McClellan SGML consulting
    Perl programming
    Fort Worth, Texas
    Tad McClellan, Mar 23, 2006
    #5
  6. Tom Regner Guest

    Reeees wrote:

    > A. Sinan Unur <> trolled:
    >> wrote in news:1143074916.738167.82330
    >> @t31g2000cwb.googlegroups.com:

    >
    >> > I would like to match lines that do NOT being with "http". Here is my
    >> > regex:
    >> >
    >> > /^[^(http)]/

    [...]
    >> >
    >> > Is this correct?

    >
    >> Of course not. You could have figured that out quite trivially yourself.

    >

    [...]
    >
    > Some of you seem to think that it is ok to insult posters simply
    > because you may, or may not, be giving them accurate technical
    > advice. But it is not ok to insult people.


    The phrase "could have trivially found that out yourself" is hardly an
    insult, and in this case a simple fact, as the only thing the OP would have
    had to do was run his code, his question (to remind you: "is this
    correct?") would have been answered immediately (the answer is "no, it
    isn't", the only correct answer, that A. Sinan Unur kindly accompanied with
    a correct solution to the problem, which wasn't asked for by th OP...)

    >
    > Please grow up. Or stop posting.

    likewise :)

    regards,
    Tom
    --
    Dievision GmbH | Kriegerstrasse 44 | 30161 Hannover
    Telefon: (0511) 288791-0 | Telefax: (0511) 288791-99
    http://www.dievision.de
    Tom Regner, Mar 23, 2006
    #6
  7. Daniel Guest

    John W. Krahn schrieb:

    > wrote:
    > > I would like to match lines that do NOT being with "http". Here is my
    > > regex:
    > >
    > > /^[^(http)]/
    > >
    > > I am using a character class with negation and then counting on the
    > > grouping to force negation of the entire "http" string.
    > >
    > > Then of course, the first ^ anchors to the beginning of the string.
    > >
    > > Is this correct?

    >
    > No, a character class is just a list of characters not a pattern so [^(http)]
    > is the same as [^()hpt]. You want a negative look-ahead assertion:
    >
    > /(?!^http)/


    You're probably more likely to need something like this:

    next if /^http/i;

    or

    return if /^http/i;

    Which is actually the oposite of what you asked.

    Daniel Cutter
    print chr--$g+ord for'KWVX%GUW]RP^-^Tb]2[UXa\j#'=~m$.$g;
    Daniel, Mar 23, 2006
    #7
  8. Reeees wrote:
    > A. Sinan Unur <> trolled:
    >> wrote in news:1143074916.738167.82330
    >> @t31g2000cwb.googlegroups.com:
    >> > I would like to match lines that do NOT being with "http". Here is
    >> > my regex:

    [...]
    >> > Is this correct?

    >
    >> Of course not. You could have figured that out quite trivially
    >> yourself.

    >
    > What is the purpose of this statement? How does this statement help
    > the user with his problem?


    It helps the user with his problem by introducing the idea of a test
    case. Whenever you don't know whether your code is correct or not, try
    to use it in a simple script which behaves differently whether it is
    correct or not.

    hp

    --
    _ | Peter J. Holzer | Löschung von at.usenet.schmankerl?
    |_|_) | Sysadmin WSR/LUGA |
    | | | | Diskussion derzeit in at.usenet.gruppen
    __/ | http://www.hjp.at/ |
    Peter J. Holzer, Mar 25, 2006
    #8
    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. Guillermo Riojas
    Replies:
    5
    Views:
    201
    Guillermo Riojas
    Nov 22, 2010
  2. MENTAT
    Replies:
    5
    Views:
    116
    John W. Krahn
    Feb 15, 2005
  3. jm
    Replies:
    5
    Views:
    83
    Tad McClellan
    Nov 2, 2006
  4. Dylan Nicholson
    Replies:
    6
    Views:
    365
    A. Sinan Unur
    Oct 19, 2007
  5. aliensite
    Replies:
    4
    Views:
    260
    aliensite
    Apr 13, 2005
Loading...

Share This Page