Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Perl Misc (http://www.velocityreviews.com/forums/f67-perl-misc.html)
-   -   Regex Matching Strings WITHOUT Chars (http://www.velocityreviews.com/forums/t886171-regex-matching-strings-without-chars.html)

Hal Vaughan 04-28-2004 12:40 AM

Regex Matching Strings WITHOUT Chars
 
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

Gunnar Hjalmarsson 04-28-2004 12:52 AM

Re: Regex Matching Strings WITHOUT Chars
 
Hal Vaughan wrote:
> How can I specify, in a regex, to NOT match "web-SL0", but to match
> all else?


!/web-SL0$/

--
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl


Paul Lalli 04-28-2004 12:56 AM

Re: Regex Matching Strings WITHOUT Chars
 
On Wed, 28 Apr 2004, Hal Vaughan wrote:

> 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

Sam Holden 04-28-2004 12:58 AM

Re: Regex Matching Strings WITHOUT Chars
 
On Wed, 28 Apr 2004 00:40:21 GMT, Hal Vaughan <hal@thresholddigital.com> wrote:
> 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.


--
Sam Holden

Bob Walton 04-28-2004 01:03 AM

Re: Regex Matching Strings WITHOUT Chars
 
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.

--
Bob Walton
Email: http://bwalton.com/cgi-bin/emailbob.pl


Hal Vaughan 04-28-2004 01:22 AM

Re: Regex Matching Strings WITHOUT Chars
 
Sam Holden wrote:

> On Wed, 28 Apr 2004 00:40:21 GMT, Hal Vaughan <hal@thresholddigital.com>
> wrote:
>> 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.
>
>


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


Gunnar Hjalmarsson 04-28-2004 02:12 AM

Re: Regex Matching Strings WITHOUT Chars
 
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?

--
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl


Hal Vaughan 04-28-2004 02:22 AM

Re: Regex Matching Strings WITHOUT Chars
 
Gunnar Hjalmarsson wrote:

> 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

Tassilo v. Parseval 04-28-2004 06:04 AM

Re: Regex Matching Strings WITHOUT Chars
 
Also sprach Purl Gurl:

> Hal Vaughan wrote:
>
> (snipped)
>
>> 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?

>
> 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
--
$_=q#",}])!JAPH!qq(tsuJ[{@"tnirp}3..0}_$;//::niam/s~=)]3[))_$-3(rellac(=_$({
pam{rekcahbus})(rekcah{lrePbus})(lreP{rehtonabus}) !JAPH!qq(rehtona{tsuJbus#;
$_=reverse,s+(?<=sub).+q#q!'"qq.\t$&."'!#+sexisexi ixesixeseg;y~\n~~dddd;eval

Gunnar Hjalmarsson 04-28-2004 09:47 AM

Re: Regex Matching Strings WITHOUT Chars
 
Hal Vaughan wrote:
> Gunnar Hjalmarsson wrote:
>> 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.


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

--
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl



All times are GMT. The time now is 08:58 AM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.