Solved

Regular expression

Posted on 2011-09-14
15
134 Views
Last Modified: 2013-11-20
I am looking for the regular expression for Date,Email,Time and a double.

Date:
RegExp("^([0-1][0-9])[\-]([0-3][0-9])[\-]([1][9][0-1][0-9])$","g");
expecting date as mm/dd/19xx
The issue is : how can I tell that
the first part : is valid only from 01 to 12?
I have it has [0-1][0-9]. Issue is I am able to give input as 19 also? how can I write the condition 13 to 19 in the above expression?

Time also I have the same question how I can tell in the regular expression except this value?

Email
0
Comment
Question by:vkchaitu82
  • 5
  • 3
  • 2
  • +2
15 Comments
 
LVL 35

Expert Comment

by:Terry Woods
ID: 36540480
Yes, ([0-1][0-9]) allows 19

(0[1-9]|1[012]) works

It's trickier for the day though as obviously 31 is valid only sometimes. Allowing up to 31, you can use:
(0[1-9]|[12][0-9]|3[01])

That gives you:
RegExp("^(0[1-9]|1[012])[\-](0[1-9]|[12][0-9]|3[01])[\-]([1][9][0-1][0-9])$","g");

I used patterns from: http://www.regular-expressions.info/dates.html
0
 
LVL 82

Expert Comment

by:leakim971
ID: 36540483
>I have it has [0-1][0-9]. Issue is I am able to give input as 19 also? how can I write the condition 13 to 19 in the above expression?

Replace it by : (0[1-9]|1[012])
0
 
LVL 35

Expert Comment

by:Terry Woods
ID: 36540485
What format do you want for time?

And for email: http://www.regular-expressions.info/email.html
0
 
LVL 35

Expert Comment

by:Terry Woods
ID: 36540559
If you don't feel like reading the linked page, a simple regex for email is:
^[A-Z0-9._%-]+@[A-Z0-9.-]+\.[A-Z]{2,4}$
0
 
LVL 1

Expert Comment

by:jimmart
ID: 36540575
^(0[1-9]|1[012])[/](0[1-9]|[12][0-9]|3[01])[/](19)\d\d$ will accept mm/dd/19yy.
^(0[1-9]|1[012])[- /.](0[1-9]|[12][0-9]|3[01])[- /.](19|20)\d\d$ will accept mm/dd/19yy or mm/dd/20yy and will accept dash (-), space ( ), slash (/), or dot (.) as dividers.
This does not disallow a date such at 02/31/1999 however.
If you are using JavaScript I would use this function instead:
function isDate(datestring) {
  if ( Object.prototype.toString.call(datestring) !== "[object Date]" )
    return false;
  return !isNaN(datestring.getTime());
}
0
 
LVL 82

Expert Comment

by:hielo
ID: 36540773
To take into account leap years, refer to:
http://www.experts-exchange.com/Programming/Languages/.NET/ASP.NET/Q_22802940.html

ID:19823352  gives you the regex for mm/dd/yyyy date format

ID:19818613 gives you the regex for dd/mm/yyyy date format
0
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 

Author Comment

by:vkchaitu82
ID: 36540978
Terry,
I am using this as you suggested:
RegExp("^(0[1-9]|1[012])[\- ](0[1-9]|[12][0-9]|3[01])[\- ]([1][9][0-1][0-9])$","g");. its not working?

Also can you  pl tell me about this. (0[1-9]|[12][0-9]|3[01])
0
 
LVL 1

Accepted Solution

by:
jimmart earned 84 total points
ID: 36542398
(0[1-9]|[12][0-9]|3[01]) means:
a 0 followed by a 1 to 9
or
1 or 2 followed by a 0 to 9
or
3 followed by a 0 or 1
0
 
LVL 35

Assisted Solution

by:Terry Woods
Terry Woods earned 166 total points
ID: 36546375
The pipe is like a logical OR for value contained within the round brackets, so

(0[1-9]|[12][0-9]|3[01])
matches pattern
0[1-9]
or pattern
[12][0-9]
or pattern
3[01]

Does that make sense?
0
 

Author Comment

by:vkchaitu82
ID: 36554702
Thanks Terry and Jummart.

Last question on this :
^[A-Z0-9._%-]+@[A-Z0-9.-]+\.[A-Z]{2,4}$

what does %,_ and - signify?
0
 

Author Comment

by:vkchaitu82
ID: 36555212
Thanks Terry and Jummart.

Last question on this :
^[A-Z0-9._%-]+@[A-Z0-9.-]+\.[A-Z]{2,4}$

what does %,_  signify
0
 
LVL 82

Expert Comment

by:leakim971
ID: 36555221
0
 
LVL 35

Assisted Solution

by:Terry Woods
Terry Woods earned 166 total points
ID: 36555991
% and _ just the literal characters.

[A-Z]{2,4} means 2 to 4 occurrences of any character between A and Z, such as:
BY
PPP
ABCD
0

Featured Post

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

Suggested Solutions

Avoid defining the variables in the global scope; trying to define them in a local function scope. Because:   • Look-up is performed every time a variable is accessed.   • Variables are resolved backwards from most specific to least specific scope…
In Part 1 (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/A_7849-Hex-Maze.html) we covered the hexagonal maze basics -- how the cells are represented in a JavaScript array and how the maze is displayed.  In this part, we'…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

760 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now