Re: split and grouping in regexp
- From: krahnj@xxxxxxxxx (John W. Krahn)
- Date: Mon, 30 Oct 2006 17:52:23 -0800
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
.
- References:
- split and grouping in regexp
- From: Daniel Kasak
- split and grouping in regexp
- Prev by Date: RE: split and grouping in regexp
- Next by Date: Re: Yet another unicode question: windows platform
- Previous by thread: RE: split and grouping in regexp
- Next by thread: Re: split and grouping in regexp
- Index(es):