Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

extract month, date, year using split

Tags:

perl

I am trying to extract date, month and year from the below string.

my $test_str = "Jan 01, 2004    Feb 01, 2004    Mar 01, 2004    Apr 01, 2004    May 01, 2004";
foreach $s (split('\t', $test_str)) {
   my ($m, $d, $y) = split('[\s|,\s]');
   print ("$m=$d=$y\n");
}

when I print the output, $y is alway empty. Am I doing something wrong? the regx I have is

[\s|,\s] # match a space or space and a comma
like image 220
jjennifer Avatar asked Apr 18 '26 22:04

jjennifer


2 Answers

Your split regex [\s|,\s] is a character class (denoted by the [] brackets), which means: "split on a single character that is either a whitespace, a pipe |, a comma, or a whitespace (again)". You will split the string Jan 01, 2004 into four strings:

"Jan"
"01"
""        # comma + whitespace creates empty string
"2004"

You also split on the $_ variable, but I assume that is a typo.

To fix your problem, change that line to:

my ($m, $d, $y) = split(/[\s,]+/, $s);

As you can see, the use of the + quantifier will strip multiple consecutive commas or whitespace.

like image 87
TLP Avatar answered Apr 22 '26 02:04

TLP


You can also do it like this: split /,?\s/, $s;.

like image 43
dan1111 Avatar answered Apr 22 '26 00:04

dan1111



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!