Re: split and grouping in regexp
- From: rvtol+news@xxxxxxxxxxxx (Dr.Ruud)
- Date: Tue, 31 Oct 2006 07:54:46 +0100
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."
.
- References:
- split and grouping in regexp
- From: Daniel Kasak
- split and grouping in regexp
- Prev by Date: Re: [SPAM DETECT] split and grouping in regexp
- Next by Date: Re: Opening .dat file in perl
- Previous by thread: Re: split and grouping in regexp
- Next by thread: Re: split and grouping in regexp
- Index(es):
Relevant Pages
|