> -----Original Message-----
> From: Stefan Rotsch [mailto:[EMAIL PROTECTED]]
> Sent: Friday, September 14, 2001 1:55 PM
> To: [EMAIL PROTECTED]
> Subject: Matching a pattern only once
> 
> 
> Hi,
> 
> I've got trouble with matching a character only once. The function
> 
> my $filename = $File::Find::name;
> if ($filename =~ /(\~){1}$/) {  
>     # do something
> }
> 
> should be used for deleting temporary files ending with a ~. Because
> of some special files ending with ~~ I would like to keep the above
> pattern has to match _exactly one_ ~ at the end of a filename.
> 
> In my eyes, {1} seems to be the right way, but it won't work. I've
> already tried moving brackets or the $-sign around, but with no
> positive result. Any hints?

The {1} is superfluous. Saying /~{1}$/ is equivalent to /~$/. This
regex matches any text whose last character is a tilde. It puts no
restriction on the preceding characters.

You can use look-behind assertion:

   /(?<!~)~$/

Which means, match a tilde, not preceded by a tilde, anchored to the
end of the string. This will match:

   foo~
   ~

But not:

   foo~~
   ~~

I'm not sure exactly which version of Perl introduced look-behind
assertion. For earlier versions that don't, this should work:

   /(?:^|[^~])~$/

Which means match either beginning of string or a non-tilde, followed
by tilde, anchored to end of string.

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to