Re: split and grouping in regexp



On Tue, 2006-10-31 at 12:22 +1100, 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.

If I just do:
my ( $yyyy, $mm, $dd ) = split /-/, $yyyymmdd;
it works for dates separated by dashes, and if I do:
my ( $yyyy, $mm, $dd ) = split /\//, $yyyymmdd;
it works for dates separted by slashes.

Why can't I do both at the same time?

--
Daniel Kasak
IT Developer
NUS Consulting Group
Level 5, 77 Pacific Highway
North Sydney, NSW, Australia 2060
T: (+61) 2 9922-7676 / F: (+61) 2 9922 7989
email: dkasak@xxxxxxxxxxxxxxxxxxxx
website: http://www.nusconsulting.com.au

You should remove the parenthesis there:

my $date = "2006-10-06";
my ($year, $month, $day) = split m{-|/}, $date;

Now you should have what you need. Using group parenthesis actually
assigns the first "match" to the $month variable, which would either be
a "-" or a "/".

.