Forum Moderators: coopster & phranque

Message Too Old, No Replies

Simplifying a list of !~ /foo/ &&

         

csdude55

3:34 pm on Dec 9, 2019 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



I have a list of filters that's about 60 lines of:

$string !~ /foo/i &&
$string !~ /bar/i &&
...


I read this suggestion, but I'm not entirely sure that it's the same thing or that I understand it properly:

$string =~ m {
^(?!.*(
foo |
bar
)).*$
}xi


I'm using m{ }xi to make it more legible, and I understand that part. What throws me off is the negative look-ahead. I guess it's sorta... match if the string doesn't contain foo or bar, but is followed by .* ?

Are these two variations doing the same thing?

My goal is to make the file a smidge smaller, and make it a little easier for me to maintain long term. Processing speed isn't a huge concern for this script unless it's a LOT slower... microseconds won't really matter, though.

phranque

6:56 am on Dec 28, 2019 (gmt 0)

WebmasterWorld Administrator 10+ Year Member Top Contributors Of The Month



$string =~ m {
^(?!.*(
foo |
bar
)).*$
}xi

not sure why you need the lookahead assertion here.
and the begin and end anchors are additional constraints that may be unwanted.
My goal is to make the file a smidge smaller, and make it a little easier for me to maintain long term.

why not this?
$string !~ /(foo|bar|...)/i;