If as both your words and code imply, you are only after xxx and xxx always has length 3 then:

print substr($var,0,3);

is much simpler and faster than any regex.

If the length of xxx can vary then

print substr($var, 0, index($var, ':'));

is almost as simple and still more efficient that a regex. (And easier to get right first time:)

If you actually want to get xxx, yyy, and zzz and they are always length 3, then

my $p=0; print substr( $var, ($p=index($var, ':', $p+1 ))-3,3),$/ whil +e $p > -1

will won't quite (see below for reason and below that for correction. do the trick. Though it is a little harder to get right first time.

The use of a regex only really comes into its own when the number, and length of the entities to be matched can both vary is a lot more efficient for even the simple cases than I thought!


Cor! Like yer ring! ... HALO dammit! ... 'Ave it yer way! Hal-lo, Mister la-de-da. ... Like yer ring!

In reply to Re: split question by BrowserUk
in thread split question by Anonymous Monk

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.