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: 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/;
}
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: fibonacci regex
by TedYoung (Deacon) on Jan 25, 2005 at 15:39 UTC | |
by ambrus (Abbot) on May 10, 2010 at 19:27 UTC |