Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

fibonacci regex

by mauke (Novice)
on Jan 25, 2005 at 13:54 UTC ( #424865=snippet: print w/ replies, xml ) Need Help??

Description: Writing your own regex library can help you understand some of the more obscure features of Perl's regexes. :-)
The following regex matches lines consisting of words whose length form the fibonacci sequence 1, 1, 2, 3, 5, 8, 13, etc. Caveat: All words must consist of the same character.

Examples of valid input:
v
x x xx xxx xxxxx

Ok, it's probably not very useful, but I thought I'd post it anyway.

#!/usr/local/bin/perl
use warnings;
use strict;

while (<>) {
    print "fibo\n" if /^\s*(?:((?(1)(?(3)(?(2)\2|\3)(\1)|(\S))|\S))\s+
+)+\z/;
}
Comment on fibonacci regex
Download Code
Replies are listed 'Best First'.
Re: fibonacci regex
by TedYoung (Deacon) on Jan 25, 2005 at 15:39 UTC

    Nice, this reminds me of a function to test if a number if prime:

    sub isPrime { ('1' x $_[0]) !~ /^(11+)\1+$/; }

    Ted Young

    ($$<<$$=>$$<=>$$<=$$>>$$) always returns 1. :-)

Back to Snippets Section

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: snippet [id://424865]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (14)
As of 2016-02-09 21:02 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How many photographs, souvenirs, artworks, trophies or other decorative objects are displayed in your home?





    Results (326 votes), past polls