For
my regex book, I have an exercise in the chapter about look-ahead that asks for a regex to split a string into groups of N characters, as though a window was being passed over the string. What I mean is, from "regexes", if N is 3, get "reg", "ege", "gex", "exe", "xes". This is rather simple once the chapter has been read -- at least, that's the purpose of the chapter!
But there is a trickier question (not in my text) which is to have the window start BEFORE the beginning of the string and end AFTER the end of the string. That means, with the same string and N, you would get "r", "re", "reg", "ege", "gex", "exe", "xes", "es", "s". That is more difficult, and sadly, bleadperl gave me grief when I tried to solve this problem with a regex using (??{ ... }). So I extend this challenge to you.
Given: string in $_, chunk size in $s (or any other variable you wish)
Golf: extract all overlapping substrings of at least 1 and at most $s characters from the string
Known: the string is at least 1 character long, but MAY be shorter than $s characters
Here is my attempt:
# 64
@s=grep''ne$_,/^@{[map"(?=(.{$_}))?",1..$s]}.|(?=(.{1,$s}))/gsx;
|
_____________________________________________________
Jeff[japhy]Pinyan:
Perl,
regex,
and perl
hacker.
s++=END;++y(;-P)}y js++=;shajsj<++y(p-q)}?print:??;