As other monks have mentioned, you can certainly use a regex in split. If you want to divide a string into parts based on field length rather than a specific delimiter (pattern), however, you can also use unpack.

use strict; use warnings; my $string = '1234 this is the remaining string'; # A4 = take the first 4 ASCII characters # x = skip the next byte # A* = take all remaining ASCII characters my ( $num, $text ) = unpack( 'A4xA*', $string ); print "[$num][$text]\n";
Prints:
[1234][this is the remaining string]

pack and unpack can be confusing. Pack/Unpack Tutorial (aka How the System Stores Data) is a great tutorial to get you started, and Super Search will help you find other references.

Update:
unpack significantly outperforms both split and the regex approach, as shown below (I used BrowserUK's code in Re: can split() use a regex? for the other two approaches):

use strict; use warnings; use Benchmark qw( cmpthese ); my $string = '1234 this is the remaining string'; cmpthese( -5, { unpack => sub { unpack( 'A4xA*', $string ) }, regex => sub { $string =~ m[(\d+)\s+(.*)$] }, split => sub { split( /(\d+)\s+/, $string, 2 ) }, } );
Results:
Rate split regex unpack split 789472/s -- -44% -70% regex 1422218/s 80% -- -45% unpack 2594984/s 229% 82% --

HTH


In reply to Re: can split() use a regex? by bobf
in thread can split() use a regex? 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.