Re: split and grouping in regexp



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

.



Relevant Pages

  • Re: newbie regexp question: how to extract just the filename from a canonical file/pathname
    ... Perl to ... >> slash, not the last one. ... My interpretation of the regular expression you gave is (hope ...
    (comp.lang.perl.misc)
  • Re: One-liner question
    ... Stephane Chazelas wrote: ... the item already has it or it starts with a slash or it is a backslash? ... I should have mentioned that those start with a slash are unchanged. ... written in sed, awk, or perl? ...
    (comp.unix.shell)
  • Re: Net::DNS
    ... function because you still use $rr within the foreach, ... > So I can find the all these object methods in the perl docs listed below? ... > the return value of 'grep'. ... > perldoc perlboot ...
    (perl.beginners)
  • Re: Posting Guidelines changes (was Re: variable interpolation of refs to anonymous subroutines)
    ... Experts at Perl or experts at posting to newsgroups? ... use warnings; # see perldoc warnings ...
    (comp.lang.perl.misc)
  • Re: Multitasking
    ... Just a comment for the archives, and to any other beginner to the Perl ... When you look at the perldoc for select, perldoc -f select, you ... select FILEHANDLE ... I just meant avoid using the "Low-level socket functions" ...
    (perl.beginners)