Cari di Perl 
    Perl User Manual
Daftar Isi
(Sebelumnya) Replace a pattern with a stringOptimize input data for repeat ... (Berikutnya)
Regular expressions and pattern matching

Split up a string using a regexp delimiter

Daftar Isi

  • split /PATTERN/,EXPR,LIMIT

  • split /PATTERN/,EXPR
  • split /PATTERN/
  • split

    Splits the string EXPR into a list of strings and returns thelist in list context, or the size of the list in scalar context.

    If only PATTERN is given, EXPR defaults to $_.

    Anything in EXPR that matches PATTERN is taken to be a separatorthat separates the EXPR into substrings (called "fields") thatdo not include the separator. Note that a separator may belonger than one character or even have no characters at all (theempty string, which is a zero-width match).

    The PATTERN need not be constant; an expression may be usedto specify a pattern that varies at runtime.

    If PATTERN matches the empty string, the EXPR is split at the matchposition (between characters). As an example, the following:

    1. print join(':', split('b', 'abc')), "\n";

    uses the 'b' in 'abc' as a separator to produce the output 'a:c'.However, this:

    1. print join(':', split('', 'abc')), "\n";

    uses empty string matches as separators to produce the output'a:b:c'; thus, the empty string may be used to split EXPR into alist of its component characters.

    As a special case for split, the empty pattern given inmatch operator syntax (//) specifically matches the empty string, which is contrary to its usualinterpretation as the last successful match.

    If PATTERN is /^/, then it is treated as if it used themultiline modifier (/^/m), since itisn't much use otherwise.

    As another special case, split emulates the default behavior of thecommand line tool awk when the PATTERN is either omitted or a literalstring composed of a single space character (such as ' ' or"\x20", but not e.g. / /). In this case, any leadingwhitespace in EXPR is removed before splitting occurs, and the PATTERN isinstead treated as if it were /\s+/; in particular, this means thatany contiguous whitespace (not just a single space character) is used asa separator. However, this special treatment can be avoided by specifyingthe pattern / / instead of the string " ", thereby allowingonly a single space character to be a separator.

    If omitted, PATTERN defaults to a single space, " ", triggeringthe previously described awk emulation.

    If LIMIT is specified and positive, it represents the maximum numberof fields into which the EXPR may be split; in other words, LIMIT isone greater than the maximum number of times EXPR may be split. Thus,the LIMIT value 1 means that EXPR may be split a maximum of zerotimes, producing a maximum of one field (namely, the entire value ofEXPR). For instance:

    1. print join(':', split(//, 'abc', 1)), "\n";

    produces the output 'abc', and this:

    1. print join(':', split(//, 'abc', 2)), "\n";

    produces the output 'a:bc', and each of these:

    1. print join(':', split(//, 'abc', 3)), "\n";
    2. print join(':', split(//, 'abc', 4)), "\n";

    produces the output 'a:b:c'.

    If LIMIT is negative, it is treated as if it were instead arbitrarilylarge; as many fields as possible are produced.

    If LIMIT is omitted (or, equivalently, zero), then it is usuallytreated as if it were instead negative but with the exception thattrailing empty fields are stripped (empty leading fields are alwayspreserved); if all fields are empty, then all fields are considered tobe trailing (and are thus stripped in this case). Thus, the following:

    1. print join(':', split(',', 'a,b,c,,,')), "\n";

    produces the output 'a:b:c', but the following:

    1. print join(':', split(',', 'a,b,c,,,', -1)), "\n";

    produces the output 'a:b:c:::'.

    In time-critical applications, it is worthwhile to avoid splittinginto more fields than necessary. Thus, when assigning to a list,if LIMIT is omitted (or zero), then LIMIT is treated as though itwere one larger than the number of variables in the list; for thefollowing, LIMIT is implicitly 4:

    1. ($login, $passwd, $remainder) = split(/:/);

    Note that splitting an EXPR that evaluates to the empty string alwaysproduces zero fields, regardless of the LIMIT specified.

    An empty leading field is produced when there is a positive-widthmatch at the beginning of EXPR. For instance:

    1. print join(':', split(/ /, ' abc')), "\n";

    produces the output ':abc'. However, a zero-width match at thebeginning of EXPR never produces an empty field, so that:

    1. print join(':', split(//, ' abc'));

    produces the output ' :a:b:c' (rather than ': :a:b:c').

    An empty trailing field, on the other hand, is produced when there is amatch at the end of EXPR, regardless of the length of the match(of course, unless a non-zero LIMIT is given explicitly, such fields areremoved, as in the last example). Thus:

    1. print join(':', split(//, ' abc', -1)), "\n";

    produces the output ' :a:b:c:'.

    If the PATTERN containscapturing groups,then for each separator, an additional field is produced for each substringcaptured by a group (in the order in which the groups are specified,as per backreferences); if any group does notmatch, then it captures the undef value instead of a substring. Also,note that any such additional field is produced whenever there is aseparator (that is, whenever a split occurs), and such an additional fielddoes not count towards the LIMIT. Consider the following expressionsevaluated in list context (each returned list is provided in the associatedcomment):

    1. split(/-|,/, "1-10,20", 3)
    2. # ('1', '10', '20')
    3. split(/(-|,)/, "1-10,20", 3)
    4. # ('1', '-', '10', ',', '20')
    5. split(/-|(,)/, "1-10,20", 3)
    6. # ('1', undef, '10', ',', '20')
    7. split(/(-)|,/, "1-10,20", 3)
    8. # ('1', '-', '10', undef, '20')
    9. split(/(-)|(,)/, "1-10,20", 3)
    10. # ('1', '-', undef, '10', undef, ',', '20')
 
Source : perldoc.perl.org - Official documentation for the Perl programming language
Site maintained by Jon Allen (JJ)     See the project page for more details
Documentation maintained by the Perl 5 Porters
(Sebelumnya) Replace a pattern with a stringOptimize input data for repeat ... (Berikutnya)