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
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.
You can also do it like this: split /,?\s/, $s;.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With