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:??;
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.