Hi all
I have a time in this format
12:00p,1:20A .
In linux i am using following regexp to check it.
egrep '[0-9]|[0-2]:(0[0-5]|1[0-9])[paPA]' $time.
I am trying to achive this in perl.
Please help me regarding this Issue....
Regards
Naveen
8 2235 prn 254
Expert 100+
Hi Naveen,
Just what are you trying to accomplish? What do you mean "check"? Are you trying to determine whether some string that purports to be a time is a potentially valid time? How did these strings get there? Why do you need to check their format? What kind of "answer" are you looking for? Are you actually trying to convert these strings to useful times or are you just trying to ensure a consistent format? What do you want to do to the ones that don't "check"?
As usual, there's more than one way to do it. Whatever "it" may be. But unless we have a somewhat better idea of what "it" is, I, at least, have no clue about what would help you.
Best Regards,
Paul
Hi Paul,
I am checking the string contain the valid time format or not as given above.
These strings are inputs to my program . If these string is in valid formate I go further like conversions to GMT,EST,CST.
Regards
Naveen.P
prn 254
Expert 100+
Hi Naveen,
So it looks like you are just asking how to tell if a string matches a regular expression. Right? - #! /usr/bin/perl
-
use strict;
-
my $input = '12:45A';
-
my $timepattern = /[0-9]|[0-2]:(0[0-5]|1[0-9])[paPA]/;
-
-
if ( $input =~ $timepattern ) {
-
print "$input looks like a time.\n";
-
}
-
else {
-
print "$input does not seem to be a time.\n";
-
}
On the other hand, if what you really want is a good way to handle strings that (may or may not) contain times in all sorts of formats, then there are good ways to do that. For example, Parsing Dates and Times from Strings is one and from a reasonably well-known source. Another possibilty for a versatile time parser is HTTP::Date, which also looks pretty versatile.
You will often find that it's better to use (or at least study) well-written packages rather than trying to roll your own. Too often people try to do things "the easy way" and fail to take all the cases into account. (I have to admit that I have often been guilty myself.) In fact, using good packages is usually even easier and generally gives you much better coverage for the unexpected cases.
Best Regards,
Paul
Paul,
You seem to be one of the "good-guys" so I wish to bring this to your attention. The link you posted: "Parsing Dates and Times from Strings" was a link to pirated material. The material seems to have been deleted but that site posts lots of pirated material.
prn 254
Expert 100+
Paul,
You seem to be one of the "good-guys" so I wish to bring this to your attention. The link you posted: "Parsing Dates and Times from Strings" was a link to pirated material. The material seems to have been deleted but that site posts lots of pirated material.
Ouch! Thanks, Kevin! I'll remember that. Now that you mention it, the URL does look suspicious. You're right. I don't want to encourage that sort of thing.
Paul
Also, I just wanted to point out that the regex that you were using would not actually validate the most basic of times. Here's one that does what I believe you intended: -
#! /usr/bin/perl
-
-
use strict;
-
-
my $input = '12:45A';
-
-
my $timepattern = /^(?:[1-9]|1[012]):[0-5][0-9][paPA]$/;
-
-
if ( $input =~ $timepattern ) {
-
print "$input looks like a time.\n";
-
-
} else {
-
print "$input does not seem to be a time.\n";
-
}
-
- Miller
I think you need to use "qr" to properly quote the regular expression otherwise it will not work. - #! /usr/bin/perl
-
use strict;
-
my $input = '12:45A';
-
my $timepattern = qr/^(?:[1-9]|1[012]):[0-5][0-9][paPA]$/;
-
if ( $input =~ $timepattern ) {
-
print "$input looks like a time.\n";
-
} else {
-
print "$input does not seem to be a time.\n";
-
}
example: - #! /usr/bin/perl
-
use strict;
-
my $input = '999991:45A';
-
my $timepattern = /^(?:[1-9]|1[012]):[0-5][0-9][paPA]$/;
-
if ( $input =~ $timepattern ) {
-
print "$input looks like a time.\n";
-
} else {
-
print "$input does not seem to be a time.\n";
-
}
output:
999991:45A looks like a time - #! /usr/bin/perl
-
use strict;
-
my $input = '999991:45A';
-
my $timepattern = qr/^(?:[1-9]|1[012]):[0-5][0-9][paPA]$/;
-
if ( $input =~ $timepattern ) {
-
print "$input looks like a time.\n";
-
} else {
-
print "$input does not seem to be a time.\n";
-
}
output:
999991:45A does not seem to be a time.
I think you need to use "qr" to properly quote the regular expression otherwise it will not work.
Nod. I was just testing you.... You passed! :)
- M
Sign in to post your reply or Sign up for a free account.
Similar topics
by: Dave Harrison |
last post by:
Ok before I start, please dont mail me telling me to use a more recent version of Python, I _would_ use 2.2.x but due to an existing framework all based on using 2.1.1 I have been specifically told...
|
by: Lentdave67t |
last post by:
Thank you in advance for any help you can provide. I am writing a C# program
that checks to see if the URLs of favorites/bookmarks are still good. The
problem I am having is that while the...
|
by: Lentdave67t |
last post by:
Thank you in advance for any help you can provide. I am writing a C# program
that checks to see if the URLs of favorites/bookmarks are still good. The
problem I am having is that while the...
|
by: Lyn |
last post by:
Hi,
I am developing a project in which I am checking for records with
overlapping start/end dates. Record dates must not overlap date of birth,
date of death, be in the future, and must not...
|
by: Mustufa Baig |
last post by:
I have an ASP.NET website where I am showing off crystal reports to users by
exporting them to pdf format. Following is the code:
----------------
1 Private Sub ExportReport()
2 Dim oStream...
|
by: Tom Becker |
last post by:
Is there a way, from Access, to programmatically click the Send and
Receive button in Outlook?
|
by: strictly_mk |
last post by:
Hi all, forgive me if there is a simple solution for this. I am going
through the following piece of code which simply calculates factorials
out of a book, but when i run it I get the answer 0 for...
|
by: Patient Guy |
last post by:
Does anyone have any coding rules they follow when doing argument checking?
When arguments fail during check, do you return from the call with an
ambiguous return value, or do you throw...
|
by: halcyon943 |
last post by:
have 4 folders that I watch and need to move files from to another
location. Three constraints:
-Finish time. Make sure the program stops transferring files at a
specific time
-Number of...
|
by: Bert |
last post by:
Problem is documented at http://ace.delos.com/usacoprob2?a=deAhyErCKsK&S=gift1
Why doesn't this work?
/*
ID: albert.4
LANG: C
TASK: gift1
*/
|
by: ryjfgjl |
last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
| |