Perl Separation Template

According to perldoc syntax for split:

split /PATTERN/,EXPR,LIMIT

But PATTERNit can also be a string with one or two quotes: split "PATTERN", EXPR. Who cares?

Change: The difference, of which I know, is split into a backslash: split /\\/vs split '\\'. The second form does not work.

+5
source share
5 answers

It looks like he uses this as an "expression to indicate patterns":

The pattern / PATTERN / can be replaced with an expression to indicate patterns that change at run time. (To execute the run-time collection only once, use / $ variable / o.)

edit: I tested it with this:

my $foo = 'a:b:c,d,e';
print join(' ', split("[:,]", $foo)), "\n";
print join(' ', split(/[:,]/, $foo)), "\n";
print join(' ', split(/\Q[:,]\E/, $foo)), "\n";

' ', .

+6

PATTERN ... , - . 1 . . , , .

'\\' . , /\/, :

C:\>perl -e "print join ':', split '\\', 'a\b\c'"
Trailing \ in regex m/\/ at -e line 1.

,

, :

  • //, .
  • ' ',   .

< > 1. inline /.../, qr//.

+2

, . .

+1
perl -e 'print join("-",split("[a-e]","regular"))';
r-gul-r

, , .

So this is basically the same - with one important exception:) split(" ",...and split(/ /,...) are different.

I prefer to use /PATTERN/, to avoid confusion, it is easy to forget that this is a regular expression otherwise.

+1
source

Two observed rules:

  • the special case is split(" ")equivalent split(/\s+/).
  • for everything else (I think, not nail me), split("something")equal tosplit(/something/)
0
source

All Articles