Regular Expressions :: yet more examples
Count Matches
1 # assign the regular expression to an array 2 # 3 # this accomplishes two things: 4 # 5 # 1. the matches are stored in a reusable data structure 6 # 2. scalar context yields the number of elements (as usual) 7 8 my $string = "Include the following states: AL, AK, AZ, AR."; 9 10 my @count = $string =~ m|\b[A-Z][A-Z]\b|g; 11 12 print "count: ", scalar(@count); 13 15 # outputs: count: 4
Split arg
1 # parse tabular data with split 2 # in addition to optional spaces, there is 3 # exactly 1 tab between between each column of data 4 5 my $string = 'Martinez, Pedro P Oct 25,1971 Pedro Jaime Martinez'; 6 7 my ($player_name, $position, $dob, $full_name) = split(m!\s*\t\s*!, $string); 8 9 # $player_name contains 'Martinez, Pedro' 10 # $position contains 'P' 11 # $dob contains 'Oct 25,1971' 12 # $full_name contains 'Pedro Jaime Martinez'