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

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

Post by weyu » Fri, 24 Mar 2006 09:48:36


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
 
 
 

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

Post by A. Sinan U » Fri, 24 Mar 2006 10:16:46


@t31g2000cwb.googlegroups.com:


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 < XXXX@XXXXX.COM >
(remove .invalid and reverse each component for email address)

comp.lang.perl.misc guidelines on the WWW:
http://www.yqcomputer.com/ ~tadmc/clpmisc/clpmisc_guidelines.html

 
 
 

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

Post by Reeee » Fri, 24 Mar 2006 10:22:43

A. Sinan Unur < XXXX@XXXXX.COM > trolled:




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
 
 
 

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

Post by John W. Kr » Fri, 24 Mar 2006 11:01:33


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
 
 
 

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

Post by Tad McClel » Fri, 24 Mar 2006 22:30:55


What happened when you tried it?


--
Tad McClellan SGML consulting
XXXX@XXXXX.COM Perl programming
Fort Worth, Texas
 
 
 

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

Post by Tom Regne » Fri, 24 Mar 2006 22:55:49


[...]
[...]

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

likewise :)

regards,
Tom
--
Dievision GmbH | Kriegerstrasse 44 | 30161 Hannover
Telefon: (0511) 288791-0 | Telefax: (0511) 288791-99
http://www.yqcomputer.com/
 
 
 

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

Post by Danie » Sat, 25 Mar 2006 02:14:43


John W. Krahn schrieb:



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;
 
 
 

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

Post by Peter J. H » Sun, 26 Mar 2006 21:32:31


[...]

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 | Lschung von at.usenet.schmankerl?
|_|_) | Sysadmin WSR/LUGA |
| | | XXXX@XXXXX.COM | Diskussion derzeit in at.usenet.gruppen
__/ | http://www.yqcomputer.com/ |