Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > Regex Matching Strings WITHOUT Chars

Reply
Thread Tools

Regex Matching Strings WITHOUT Chars

 
 
Hal Vaughan
Guest
Posts: n/a
 
      04-28-2004
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
 
Reply With Quote
 
 
 
 
Gunnar Hjalmarsson
Guest
Posts: n/a
 
      04-28-2004
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

 
Reply With Quote
 
 
 
 
Paul Lalli
Guest
Posts: n/a
 
      04-28-2004
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
 
Reply With Quote
 
Sam Holden
Guest
Posts: n/a
 
      04-28-2004
On Wed, 28 Apr 2004 00:40:21 GMT, Hal Vaughan <(E-Mail Removed)> 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
 
Reply With Quote
 
Bob Walton
Guest
Posts: n/a
 
      04-28-2004
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

 
Reply With Quote
 
Hal Vaughan
Guest
Posts: n/a
 
      04-28-2004
Sam Holden wrote:

> On Wed, 28 Apr 2004 00:40:21 GMT, Hal Vaughan <(E-Mail Removed)>
> 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

 
Reply With Quote
 
Gunnar Hjalmarsson
Guest
Posts: n/a
 
      04-28-2004
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

 
Reply With Quote
 
Hal Vaughan
Guest
Posts: n/a
 
      04-28-2004
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
 
Reply With Quote
 
Tassilo v. Parseval
Guest
Posts: n/a
 
      04-28-2004
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
 
Reply With Quote
 
Gunnar Hjalmarsson
Guest
Posts: n/a
 
      04-28-2004
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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Regex testing and UTF8 awarenes or Regex and numeric pattern matching sln@netherlands.com Perl Misc 2 03-10-2009 03:51 AM
String Pattern Matching: regex and Python regex documentation Xah Lee Python 8 09-26-2006 03:24 PM
String Pattern Matching: regex and Python regex documentation Xah Lee Perl Misc 2 09-25-2006 03:15 AM
String Pattern Matching: regex and Python regex documentation Xah Lee Java 1 09-22-2006 07:11 PM
Floats to chars and chars to floats Kosio C Programming 44 09-23-2005 09:49 AM



Advertisments