Regular Expressions
Split with Regex
split can use a regex separator to break text into fields.
split
A split turns one string into fields wherever the separator pattern matches.
Split with Regex
split_regex.pl
Replay: real traced execution (multi-file project)
use strict;
use warnings;
my $line = "apple pear plum";
my @words = split /\s+/, $line;
my $first = $words[0];
my $count = scalar @words;
print "line=$line\n";
print "first=$first\n";
print "count=$count\n";
use strict;
use warnings;
my $line = "cat dog";
my @words = split /\s+/, $line;
my $first = $words[0];
my $count = scalar @words;
print "line=$line\n";
print "first=$first\n";
print "count=$count\n";
use strict;
use warnings;
my $line = "one two three";
my @words = split /\s+/, $line;
my $first = $words[0];
my $count = scalar @words;
print "line=$line\n";
print "first=$first\n";
print "count=$count\n";
$line ← apple pear plum, @words ← 3, $first ← apple, $count ← 3
4my $line→ apple pear plum = "apple pear plum"; #@line="cat dog", "one two three"5my @words→ 3 = split /\s+/, $lineapple pear plum;6my $first→ apple = $words[0]apple;7my $count→ 3 = scalar @words3;89print "line=$lineapple pear plum\n";10print "first=$firstapple\n";11print "count=$count3\n";outputline=apple pear plum first=apple count=3
$line ← cat dog, @words ← 2, $first ← cat, $count ← 2
4my $line→ cat dog = "cat dog";5my @words→ 2 = split /\s+/, $linecat dog;6my $first→ cat = $words[0]cat;7my $count→ 2 = scalar @words2;89print "line=$linecat dog\n";10print "first=$firstcat\n";11print "count=$count2\n";outputline=cat dog first=cat count=2
$line ← one two three, @words ← 3, $first ← one, $count ← 3
4my $line→ one two three = "one two three";5my @words→ 3 = split /\s+/, $lineone two three;6my $first→ one = $words[0]one;7my $count→ 3 = scalar @words3;89print "line=$lineone two three\n";10print "first=$firstone\n";11print "count=$count3\n";outputline=one two three first=one count=3