Re: split and grouping in regexp



Daniel Kasak wrote:
I'm trying to split a date where the values can be separated by a dash
'-' or a slash '/', eg:
2006-10-31 or 2006/10/31

I'm using:
my ( $yyyy, $mm, $dd ) = split /(-|\/)/, $yyyymmdd;
but it doesn't work.

Yes it does work, it's just that the capturing parentheses are returning the
contents of the capturing parentheses so '2006/10/31' is returning the list (
'2006', '/', '10', '/', '31' ). You need to use a character class:

my ( $yyyy, $mm, $dd ) = split /[-\/]/, $yyyymmdd;

Or non-capturing parentheses:

my ( $yyyy, $mm, $dd ) = split /(?:-|\/)/, $yyyymmdd;

Or you could just return the numbers you want instead of removing the
non-numbers you don't want:

my ( $yyyy, $mm, $dd ) = $yyyymmdd =~ /\d+/g;




John
--
Perl isn't a toolbox, but a small machine shop where you can special-order
certain sorts of tools at low cost and in short order. -- Larry Wall
.