On 11-04-08 03:12 AM, Anirban Adhikary wrote:
my  $x = '12abc34bf5';
  @num = split /(a|b)+/, $x;
print "NUM=@num\n";
NUM=12 b c34 b f5

`split` normally splits a string by separating the string into segments by the regular expression. The following split a string into "words", that is, text segments without any white space:

#!/usr/bin/env perl

use strict;
use warnings;

use Data::Dumper;

# Make Data::Dumper pretty
$Data::Dumper::Sortkeys = 1;
$Data::Dumper::Indent   = 1;

# Set maximum depth for Data::Dumper, zero means unlimited
local $Data::Dumper::Maxdepth = 0;

my $string = "The quick brown fox jumped over the lazy dogs.";

my @words = split /\s+/, $string;
print '@words: ', Dumper \@words;

__END__

But if you put the regular expression in parentheses, it also returns what matches:

#!/usr/bin/env perl

use strict;
use warnings;

use Data::Dumper;

# Make Data::Dumper pretty
$Data::Dumper::Sortkeys = 1;
$Data::Dumper::Indent   = 1;

# Set maximum depth for Data::Dumper, zero means unlimited
local $Data::Dumper::Maxdepth = 0;

my $string = "The quick brown fox jumped over the lazy dogs.";

my @words_and_spaces = split /(\s+)/, $string;
print '@words_and_spaces: ', Dumper \@words_and_spaces;

__END__

Because of the parentheses in your regex, it will capture what is the match but since the plus is outside, it will only capture the last match.

To get it to capture the sequence of a's and b's, use:

@num = split /((?:a|b)+)/, $x;

To get it to not capture any matches, use the non-capture parentheses:

@num = split /(?:(?:a|b)+)/, $x;

See:
perldoc -f split
perldoc perlretut
perldoc perlre


--
Just my 0.00000002 million dollars worth,
  Shawn

Confusion is the first step of understanding.

Programming is as much about organization and communication
as it is about coding.

The secret to great software:  Fail early & often.

Eliminate software piracy:  use only FLOSS.

--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/


Reply via email to