For Programmers: Free Programming Magazines  


Home > Archive > PERL Beginners > October 2006 > Re: split and grouping in regexp









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Re: split and grouping in regexp
Dr.Ruud

2006-10-31, 3:57 am

Daniel Kasak schreef:

> 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.


It does work, but not as you expected.
Read `perldoc -f split` again, look for "parentheses".

perl -wle'
$_ = "1-2/3";
print for split "(-|/)"
'

perl -wle'
$_ = q{1-2/3};
print for split /[[:punct:]]/
'

perl -wle'
$_ = q{1-2/3};
print for m/([0-9]+)/g
'


With a backreference, you can have it only match when the delimiters are
the same:

perl -wle'
$_ = q{1-2-3};
my ($yyyy, $delim, $mm, $dd) = m/([0-9]+)(.)([0-9]+)\2([0-9]+)/;
print for ($delim, $yyyy, $mm, $dd)
'

--
Affijn, Ruud

"Gewoon is een tijger."

Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2009 codecomments.com