Do you remember my Unique-Character Substring post from many months ago? Well, today, ihb- and tybalt89 posed the same challenge on DALnet #perl. Except they were looking for a regex to do the trick. The shortest one. Fore!

So then they asked me, since they know of my reputation as a regex-maniac. I tried several approaches, and they were all far over par. (At first, ihb- said it was 60, but he'd dropped \Q\E somewhere, so par is 64.)

Then I was struck by the easiest solution. Match a character, and then match a dynamic character class of anything BUT what you just matched. This dynamic character class is generated via (??{ ... }).

In the end, he and I had the exact same code, developed independently -- for, while I had asked him "did you use sort?" and "did you use look-ahead?" and "did you remember /s?", his answers were all yes, and I had already crafted my answer. I found it scarily odd that we had converged on the one way to do it (insomuchas it is the most compact solution, and represents the standard algorithm used).
(sort{length$b<=>length$a}pop=~/(?=(.(??{"[^\Q$&\E]"})*))/gs)[0]


japhy -- Perl and Regex Hacker

In reply to TOOWTDI (There's Only One Way To Do It) by japhy

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • 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:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.