Regex Matching Strings WITHOUT Chars

H

Hal Vaughan

I'm using Archive::Zip and I can specify, with a regex, the filenames that
are added to an archive. I have two types of log files, distinguishable by
name types. One ends with "-web-SL0" and the other ends with
"-server-SL0". How can I specify, in a regex, to NOT match "web-SL0", but
to match all else?

I tried a few ideas, like specifying to match it 0 times:

/(web-SL0){0}/

but it didn't work. I also tried various combinations, like match all chars
OR zero occurances of web-SL0, but nothing worked. I've looked through all
my references, but I can't find a way to specify to NOT match a pattern in
a regex. Is it possible?

Thanks!

Hal
 
P

Paul Lalli

I'm using Archive::Zip and I can specify, with a regex, the filenames that
are added to an archive. I have two types of log files, distinguishable by
name types. One ends with "-web-SL0" and the other ends with
"-server-SL0". How can I specify, in a regex, to NOT match "web-SL0", but
to match all else?

I tried a few ideas, like specifying to match it 0 times:

/(web-SL0){0}/

but it didn't work. I also tried various combinations, like match all chars
OR zero occurances of web-SL0, but nothing worked. I've looked through all
my references, but I can't find a way to specify to NOT match a pattern in
a regex. Is it possible?


There's a few ways. In this case, I'd say a negative lookbehind assertion
is your best bet:

/(?<!web-SL0)$/

This says "Match the end of the string so long as it is NOT preceeded by
"web-SL0".

You can read up on this feature by searching for 'look-behind' in
perldoc perlre

Hope this helps,
Paul Lalli
 
S

Sam Holden

I'm using Archive::Zip and I can specify, with a regex, the filenames that
are added to an archive. I have two types of log files, distinguishable by
name types. One ends with "-web-SL0" and the other ends with
"-server-SL0". How can I specify, in a regex, to NOT match "web-SL0", but
to match all else?

I tried a few ideas, like specifying to match it 0 times:

/(web-SL0){0}/

That will just match everything, after all all strings contain a substring
(of zero length) containing no copies of 'web-SL0'.
but it didn't work. I also tried various combinations, like match all chars
OR zero occurances of web-SL0, but nothing worked. I've looked through all
my references, but I can't find a way to specify to NOT match a pattern in
a regex. Is it possible?

If the "end with" statement in the description is correct then:

/(?<!-web-SL0)$/

See the
perldoc perlre
documentation - search for "look-behind" for the description of the syntax.
 
B

Bob Walton

Hal Vaughan wrote:

....

"-server-SL0". How can I specify, in a regex, to NOT match "web-SL0", but
to match all else? ....


my references, but I can't find a way to specify to NOT match a pattern in
a regex. Is it possible? ....


Hal

Check out:

perldoc perlre

in particular looking for text that says "zero-width negative look-ahead
assertion" and "zero-width negative look-behind assertion". By properly
applying one of those, you should be able to accomplish your task.
 
H

Hal Vaughan

Sam said:
That will just match everything, after all all strings contain a substring
(of zero length) containing no copies of 'web-SL0'.

If the "end with" statement in the description is correct then:

/(?<!-web-SL0)$/

See the
perldoc perlre
documentation - search for "look-behind" for the description of the
syntax.

Thanks. I also got a private reply suggesting the same topic.  I had not
tried perlre -- tried perldoc -q regex, and variations on
perldoc regex, re, or "regular expression", but perlre has stuff the others
don't.  I had not even realized there would be more info specifically for
perlre instead of regex or re -- just never thougth to add "perl" to it!

That'll do it.

Hal
 
G

Gunnar Hjalmarsson

I don't understand why everyone (except me) suggest solutions with
extended regex patterns. It may be the most accurate answer to OP's
literal question, but don't you forget the context? You don't need any
extended pattern to exclude filenames matching a pattern when
archiving files, do you? And isn't !/string/ an easier solution that
would be preferable in this case?
 
H

Hal Vaughan

Gunnar said:
I don't understand why everyone (except me) suggest solutions with
extended regex patterns. It may be the most accurate answer to OP's
literal question, but don't you forget the context? You don't need any
extended pattern to exclude filenames matching a pattern when
archiving files, do you? And isn't !/string/ an easier solution that
would be preferable in this case?

Actually, your solution is much simpler (as to which is faster, I would not
know). But, as an additional point, I am self taught, and learning about
look-behinds will be helpful, not just for this context, but in other
contexts.

In this particular case, using !/string/ was simpler and what I used in for
this particular problem. However, there are some other problems I'm
working on with regexes, and for those, the look-behind will be a HUGE help
for me.

I also had missed a helpful topic in Perldoc, and the public responses
pointed that out (and a private response suggested something so obvious
that I had never thought of it -- to use "perldoc perldoc"). The holes in
one's experience if you're self-taught can be very frustrating, at times.

Hal
 
T

Tassilo v. Parseval

Also sprach Purl Gurl:
Hal Vaughan wrote:

(snipped)


if (index ($input, "-server-SL0") > -1)
{ next; }

Your solution will also identify those files where "-server-SL0" shows
up somewhere in the middle of the string.

According to you, your solutions are always obeying the specifications.
Here however, it mysteriously doesn't.

Tassilo
 
G

Gunnar Hjalmarsson

Hal said:
Actually, your solution is much simpler (as to which is faster, I
would not know). But, as an additional point, I am self taught,
and learning about look-behinds will be helpful, not just for this
context, but in other contexts.

In this particular case, using !/string/ was simpler and what I
used in for this particular problem. However, there are some other
problems I'm working on with regexes, and for those, the
look-behind will be a HUGE help for me.

Okay, Hal, it's good that you (and others) will benefit from their
mentioning of extended patterns. It's just that people who answer here
typically take great pains in finding the most suitable method for
addressing OP's question, and I began to wonder if I had missed
something. But since nobody has claimed that extended patterns are
necessary, I suppose not.
The holes in one's experience if you're self-taught can be very
frustrating, at times.

Yeah, indeed they can. ;-)
 
H

Hal Vaughan

Gunnar said:
Okay, Hal, it's good that you (and others) will benefit from their
mentioning of extended patterns. It's just that people who answer here
typically take great pains in finding the most suitable method for
addressing OP's question, and I began to wonder if I had missed
something. But since nobody has claimed that extended patterns are
necessary, I suppose not.

That's a very good point. In this case, I'm quite lucky, because both
responses helped me. But you make a valid point. Perl programmers seem to
be a special breed, and they're definitely hackers. To that mindset, the
more unique and obscure a piece of code is, the more interesting it is,
and, therefore, the better it is.

But I can't complain -- like I said, both answers were a HUGE help to me.

Hal
 
H

Hal Vaughan

Purl said:
Hal Vaughan wrote:

(snipped)


if (index ($input, "-server-SL0") > -1)
{ next; }


Purl Gurl

Nope, not at all.

Forgot the regex part (in the original post and question -- and even in the
title) in your eagerness?

Hal
 
R

Richard Morse

Hal Vaughan said:
Thanks. I also got a private reply suggesting the same topic.  I had not
tried perlre -- tried perldoc -q regex, and variations on
perldoc regex, re, or "regular expression", but perlre has stuff the others
don't.  I had not even realized there would be more info specifically for
perlre instead of regex or re -- just never thougth to add "perl" to it!

If you type 'perldoc perl', you will get a list of all the various
included docs, where you can find perlrequick, perlretut, perlfaq6, and
perlre, among many others...

HTH,
Ricky
 
H

Hal Vaughan

Purl said:
Irrelevant. Your comments do not comply with the
originating author's stated parameters.

Nope. He is right on target. You are irrelevant. You are just to busy
blaming everyone else to notice that there's a difference between showing
off and helping a poster.

On my old system I had a filter in place to kill Purl Gurl posts, since I
found they were 1) always sassy, 2) focused on showing off, 3) concerned
with making Purl Gurl look good by making others look bad, 4) often
irrelevant because it was more important for Purl Gurl to look good than to
help.

Time to set up that filter again.

Hal
 
H

Hal Vaughan

Purl said:
Please provide a source reference for this rule
which states alternative code cannot used.

I stated the needs, and said I could specify with a regex. I did not say
with a regex or something else. Do you read posts completely before
answering or do you just post quickly, to show off, without regard to
whether your post is actually helpful? What is your honest goal? To show
off, to look good, to impress people, or to help them? Your posts indicate
it is the first 3 and not the last one.
You lack imagination.

Nope, you lack the ability to pay attention to the parameters presented and
are more interested in looking good than helping others.

(That's a funny thing to say to someone who used to deal with the producers
of a sci-fi tv show and had story ideas turned down because they went too
far beyond the "normal" mold....)

Hal
 

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

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,013
Latest member
KatriceSwa

Latest Threads

Top