473,386 Members | 1,810 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Please help with regular expression

Hi,

I have the following regular expression to validate a date-time field
in European or d/m/y h:m:s format.

^((((31\/(0?[13578]|1[02]))|((29|30)\/(0?[1,3-9]|1[0-2])))\/(1[6-9]|[2-9]\d)?\d{2})|(29\/0?2\/(((1[6-9]|[2-9]\d)?(0[48]|[2468][048]|[13579][26])|((16|[2468][048]|[3579][26])00))))|(0?[1-9]|1\d|2[0-8])\/((0?[1-9])|(1[0-2]))\/((1[6-9]|[2-9]\d)?\d{2}))
(20|21|22|23|[0-1]?\d):[0-5]?\d:[0-5]?\d$

I wish to ammend it so that the time or h:m:s part is validated only if
supplied (i.e. I wish to make the time part optional). At present if a
time is not supplied the validation fails. I gather that the following
part of the above expression

(20|21|22|23|[0-1]?\d):[0-5]?\d:[0-5]?\d$

validates the time, however I don't know the syntax to make it
optional. I hope somebody can help.

Thanks,

Paul

Jul 23 '05 #1
6 2005
Ivo
<pa********@hotmail.com> wrote >
I have the following regular expression to validate a date-time field
in European or d/m/y h:m:s format.

^((((31\/(0?[13578]|1[02]))|((29|30)\/(0?[1,3-9]|1[0-2])))\/(1[6-9]|[2-9]\d)
?\d{2})|(29\/0?2\/(((1[6-9]|[2-9]\d)?(0[48]|[2468][048]|[13579][26])|((16|[2
468][048]|[3579][26])00))))|(0?[1-9]|1\d|2[0-8])\/((0?[1-9])|(1[0-2]))\/((1[
6-9]|[2-9]\d)?\d{2})) (20|21|22|23|[0-1]?\d):[0-5]?\d:[0-5]?\d$

I wish to ammend it so that the time or h:m:s part is validated only if
supplied (i.e. I wish to make the time part optional). At present if a
time is not supplied the validation fails. I gather that the following
part of the above expression

(20|21|22|23|[0-1]?\d):[0-5]?\d:[0-5]?\d$

validates the time, however I don't know the syntax to make it
optional. I hope somebody can help.


Put brackets () around the part you quoted minus the dollar sign, and put a
question mark after the closing bracket. Like so:

((20|21|22|23|[0-1]?\d):[0-5]?\d:[0-5]?\d)?$

The brackets group the characters so the question mark applies to the whole
thing. The meaning of the question mark is exactly what you ask for: the
preceding bit is optional. The dollar sign matches the end of the string so
applies to the whole regex and should not be inside this group.

hth
--
Ivo
http://4umi.com/web/regex/

Jul 23 '05 #2
JRS: In article <11**********************@f14g2000cwb.googlegroups .com>
, dated Tue, 12 Apr 2005 07:50:08, seen in news:comp.lang.javascript,
pa********@hotmail.com posted :
I have the following regular expression to validate a date-time field
in European or d/m/y h:m:s format.

^((((31\/(0?[13578]|1[02]))|((29|30)\/(0?[1,3-9]|1[0-2])))\/(1[6-9]|[2-9]\d)?\d{
2})|(29\/0?2\/(((1[6-9]|[2-9]\d)?(0[48]|[2468][048]|[13579][26])|((16|[2468][048
]|[3579][26])00))))|(0?[1-9]|1\d|2[0-8])\/((0?[1-9])|(1[0-2]))\/((1[6-9]|[2-9]\d
)?\d{2}))
(20|21|22|23|[0-1]?\d):[0-5]?\d:[0-5]?\d$

I wish to ammend it so that the time or h:m:s part is validated only if
supplied (i.e. I wish to make the time part optional). At present if a
time is not supplied the validation fails. I gather that the following
part of the above expression

(20|21|22|23|[0-1]?\d):[0-5]?\d:[0-5]?\d$

validates the time, however I don't know the syntax to make it
optional. I hope somebody can help.


You *could* omit the final $ from the RegExp without *much* loss; then
you can append " 00:00:00" to the date or date/time to make it a
date/time or date/time/time, with only the first time being used
There are much more sensible approaches - well, at least one - to
validating date and/or time. Read the newsgroup FAQ (Mon, Fri, or WWW
edition), and see below, and read <URL:http://www.merlyn.demon.co.uk/js-
date4.htm>.

Of course, d/m/y is not really European; some use d-m-y, some d/m/y,
some d.m.y, some y-m-d.

If you can, however, use YYYY-MM-DD and hh:mm:ss, as per ISO 8601 -
note, any field numerically 0-9 needs a leading zero.
I suggest starting with split(" ") or maybe split(/[^0-9:-]+/) to
break date & time apart, check length to see if there should be a time,
and validate separately.

A significant part of the RegExp above is dealing with the 100 & 400
year Gregorian Rules. That seems unlikely to be needed at present,
since between 1900-03-01 and 2100-02-28 inclusive the Gregorian Calendar
gas the same dates as the Julian (13 days earlier); only the 4-year rule
is of current importance.

--
© John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 IE 4 ©
<URL:http://www.jibbering.com/faq/> JL/RC: FAQ of news:comp.lang.javascript
<URL:http://www.merlyn.demon.co.uk/js-index.htm> jscr maths, dates, sources.
<URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.
Jul 23 '05 #3
Hi Ivo,

Thanks for the response - its much appreciated. I got it to work.

Paul

Jul 23 '05 #4
Hi John,

Thanks for the reply. I know its not perfect but I got it to wok in the
end. Thanks for your help.

Paul

Jul 23 '05 #5
Hi John,

Thanks for the reply. I know its not perfect but I got it to work in
the end. Thanks for your help.

Paul

Jul 23 '05 #6
Hi John,

Thanks for the reply. I know its not perfect but I got it to work in
the end. Thanks for your help.

Paul

Jul 23 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Neri | last post by:
Some document processing program I write has to deal with documents that have headers and footers that are unnecessary for the main processing part. Therefore, I'm using a regular expression to go...
4
by: sasifiqbal | last post by:
Hi All, I have following text Template that needs to be parsed using Regular Expression -- Test Template This is a Test Template for <#OrderNumber/> Sender Message is <#SENDERMESSAGE/>
17
by: Mark | last post by:
I must create a routine that finds tokens in small, arbitrary VB code snippets. For example, it might have to find all occurrences of {Formula} I was thinking that using regular expressions...
4
by: MooMaster | last post by:
I'm trying to develop a little script that does some string manipulation. I have some few hundred strings that currently look like this: cond(a,b,c) and I want them to look like this: ...
1
by: NvrBst | last post by:
I want to use the .replace() method with the regular expression /^ %VAR % =,($|&)/. The following DOESN'T replace the "^default.aspx=,($|&)" regular expression with "":...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
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...
0
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
1
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...
0
marktang
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,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
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,...
0
jinu1996
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 using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.