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

W

weyus

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
 
A

A. Sinan Unur

(e-mail address removed) wrote in @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
 
R

Reeees

A. Sinan Unur said:
(e-mail address removed) wrote in @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
 
J

John W. Krahn

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
 
T

Tad McClellan

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?
 
T

Tom Regner

Reeees said:
A. Sinan Unur said:
(e-mail address removed) wrote in @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
 
D

Daniel

John said:
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;
 
P

Peter J. Holzer

Reeees said:
A. Sinan Unur said:
(e-mail address removed) wrote in @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
 

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

No members online now.

Forum statistics

Threads
473,756
Messages
2,569,535
Members
45,008
Latest member
obedient dusk

Latest Threads

Top