[tpm] looking for a regex that returns true on no match

Mike Stok mike at stok.ca
Thu Mar 12 09:16:16 PDT 2009


On Mar 12, 2009, at 11:17 AM, Mike Stok wrote:

> I've deleted the rest of the thread, but what about
>
> $string =~ /^(?:(?!good).)*$/
>
> to check that $string doesn't contain good?
>
>   DB<1> x 'bad' =~ /^(?:(?!good).)*$/
> 0  1
>   DB<2> x 'good enough' =~ /^(?:(?!good).)*$/
>   empty array
>
> Mike

Note that I posted this after discovering it at http://www.kimgentes.com/worshiptech-web-tools-page/2008/12/20/regular-expressions-except-a-given-string-negative-patterns.html 
  which ends with:

*YOU ARE FREE to use this algorithm in any application (commercial or  
personal or whatever). It comes with no warrantees.  If you DO end up  
using this REGEX pattern, please do so with the following  
considerations:
make this notation in your source code:  ©2008 Kim Anthony Gentes -  
FREE TO USE ANYWHERE.
Please post a response on this blog entry below (you do that by  
clicking on the "Comments" link at the bottom of this entry), saying  
you found this and are using it. I'd just like to know if its helping  
people and how people are using it.
When using the regex, some important things to know:
Options (turned on in your language/utility): ^ and $ match at line  
breaks

so I don't claim to have the brains to invent it or want to claim  
credit for it (merely the use of the appropriate Google search terms...)

It is a fun exercise to work through it to see how it works...

Mike

> On Mar 12, 2009, at 10:44 AM, Fulko Hew wrote:
>
>>
>> On Thu, Mar 12, 2009 at 10:30 AM, Adam Prime  
>> <adam.prime at utoronto.ca> wrote:
>>
>> Fulko Hew wrote:
>> Alex asked:
>>
>>  > Couldn't you just use the ~! on the regex instead of ~=?
>>
>> ... snip ...
>>
>>
>> it's !~, not ~!
>>
>> ie:
>>
>> aprime at primepc:~$ cat haha.pl
>>
>> my $text = 'haha';
>>
>> if ($text !~ /hat/) {
>>    print qq[yay\n];
>> }
>> else {
>>    print qq[yay\n];
>> }
>> aprime at primepc:~$ perl haha.pl
>> yay
>>
>> also, given your example there, why wouldn't you just
>>
>> return true unless $_ eq 'good';
>>
>>
>> Because I'm not allowed to rewrite the code, but simply insert a
>> different regex pattern in between the slashes.
>> _______________________________________________
>> toronto-pm mailing list
>> toronto-pm at pm.org
>> http://mail.pm.org/mailman/listinfo/toronto-pm
>
> -- 
>
> Mike Stok <mike at stok.ca>
> http://www.stok.ca/~mike/
>
> The "`Stok' disclaimers" apply.
>
>
>
>
> _______________________________________________
> toronto-pm mailing list
> toronto-pm at pm.org
> http://mail.pm.org/mailman/listinfo/toronto-pm

-- 

Mike Stok <mike at stok.ca>
http://www.stok.ca/~mike/

The "`Stok' disclaimers" apply.




-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.pm.org/pipermail/toronto-pm/attachments/20090312/3d981d62/attachment.html>


More information about the toronto-pm mailing list