Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Program Won't Record Dates Such as 9/23/2004

Posted on 2004-09-26
8
Medium Priority
?
227 Views
Last Modified: 2010-04-20
I Have this typed in my perl code:
use CGI qq(:standard escapeHTML);

if ($date=~ /\d\d?[-/]/d/d?[-/]\d\d\d\d/){
do this......
this......
and this.....
}

It works when I have 9-23-2004 but not when I have 9/23/2004

I have a subroutine that changes the date to a format mysql understands. 2004-09-22

I have tried escaping / with \/ but it still doesn''work
0
Comment
Question by:lynn402
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
  • 2
8 Comments
 
LVL 40

Expert Comment

by:jlevie
ID: 12156476
Try "if($date =~ /\d\d-\d\d-\d\d\d\d/ || $date =~ /\d\d\/\d\d\/\d\d\d\d/)"
0
 
LVL 4

Expert Comment

by:EinarTh
ID: 12159288
Correct expression is:

/\d\d?[-\/]\d\d?[-\/]\d\d\d\d/

cheers.
0
 
LVL 4

Expert Comment

by:EinarTh
ID: 12159297
or even:

/(\d{1,2}[-\/]){2}\d{4}/

cheers
0
Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

 

Author Comment

by:lynn402
ID: 12175559
It still doesn't work, EinarTh, I've tried \d\d?[-\/] and it never worked. jlieve, your suggestion didn't work either.
0
 

Author Comment

by:lynn402
ID: 12175743
I'm using Perl's CGI.pm

use CGI; qq(:starndard );
#the date is located inside  query_string
$date=query_string;


print $date
#before the if statement executed I printed $date and got when the date was 9/28/2004
9%2F18%2F2004
#How can I  change the hex info back to the / so it will get recognized

if ($date=~ /\d\d?[-/]/d/d?[-/]\d\d\d\d/){
do this......
this......
and this.....
}
0
 
LVL 40

Expert Comment

by:jlevie
ID: 12175813
Oops, I just realized that my comment wasn't exactly right for the data you show in the question. It should have been:

if($date =~ /\d{1,2}-\d{1,2}-\d{2,4}/ || $date =~ /\d{1,2}\/\d{1,2}\/\d{2,4}/)

I was looking at the SQL date representation which uses 2 digit month & day and 4 digit years. The if statement above is correct in the general case as shown by:

#!/usr/bin/perl
$date[0] = "9-27-2004";
$date[1] = "09-27-04";
$date[2] = "9/28/2004";
$date[3] = "09/1/04";

for($i=0; $i<=3; $i++)
{
  $date = $date[$i];
  if($date =~ /\d{1,2}-\d{1,2}-\d{2,4}/ || $date =~ /\d{1,2}\/\d{1,2}\/\d{2,4}/)
  {
    print "Date ($date) valid\n";
  }
  else
  {
    print "Date ($date) invalid\n";
  }
}

chaos> perl dates-check
Date (9-27-2004) valid
Date (09-27-04) valid
Date (9/28/2004) valid
Date (09/1/04) valid
0
 
LVL 40

Expert Comment

by:jlevie
ID: 12175826
> 9%2F18%2F2004
> #How can I  change the hex info back to the / so it will get recognized

$date =~ s/%2F/\//g;
0
 
LVL 4

Accepted Solution

by:
EinarTh earned 200 total points
ID: 12181223
Sorry, didn't notice you url-encoded the dates.

There is a cgi function to url-decode it, but I dont have perl in front of me right now.

In any case you can:

$date =~ s/%2F/\//gi;

and then use the expression in the previous comment.
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

If you have a server on collocation with the super-fast CPU, that doesn't mean that you get it running at full power. Here is a preamble. When doing inventory of Linux servers, that I'm administering, I've found that some of them are running on l…
I. Introduction There's an interesting discussion going on now in an Experts Exchange Group — Attachments with no extension (http://www.experts-exchange.com/discussions/210281/Attachments-with-no-extension.html). This reminded me of questions tha…
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial
Suggested Courses
Course of the Month11 days, 17 hours left to enroll

636 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question