Solved

c# Regex Valid Dates (without following digits )

Posted on 2014-02-26
8
508 Views
Last Modified: 2014-02-26
I am using Regex Magic but I cannot isolate the Valid Dates.
 My regular expression for this is
(?:3[01]|[12][0-9]|0?[1-9])[/.-](?:1[0-2]|0?[1-9])|(?:1[0-2]|0?[1-9])[/.-](?:Jan|January|Feb|February|Mar|March|Apr|April|May|May|Jun|June|Jul|July|Aug|August|Sep|September|Oct|October|Nov|November|Dec|December)|(?:3[01]|[12][0-9]|0?[1-9])[ \t]+(?:Jan|January|Feb|February|Mar|March|Apr|April|May|May|Jun|June|Jul|July|Aug|August|Sep|September|Oct|October|Nov|November|Dec|December)

This was enough for me if I had not the following issue

1.95 with my regex returns 1.9 but this is not valid date for me because has a digit following it. I think there is a way to prevent this but I cannot get there!
thks
0
Comment
Question by:novreisb
  • 4
  • 3
8 Comments
 
LVL 84

Expert Comment

by:ozo
ID: 39890760
(?:1[0-2]|0?[1-9])$
0
 
LVL 35

Accepted Solution

by:
Terry Woods earned 500 total points
ID: 39890770
Added a negative lookahead (?!\d) after the numeric month pattern:
(?:3[01]|[12][0-9]|0?[1-9])[/.-](?:1[0-2]|0?[1-9])(?!\d)|(?:1[0-2]|0?[1-9])[/.-](?:Jan|January|Feb|February|Mar|March|Apr|April|May|May|Jun|June|Jul|July|Aug|August|Sep|September|Oct|October|Nov|November|Dec|December)|(?:3[01]|[12][0-9]|0?[1-9])[ \t]+(?:Jan|January|Feb|February|Mar|March|Apr|April|May|May|Jun|June|Jul|July|Aug|August|Sep|September|Oct|October|Nov|November|Dec|December)

Open in new window


You may want to ensure there's no number preceding it either, with a negative lookbehind:
(?!<\d)(?:3[01]|[12][0-9]|0?[1-9])[/.-](?:1[0-2]|0?[1-9])(?!\d)|(?:1[0-2]|0?[1-9])[/.-](?:Jan|January|Feb|February|Mar|March|Apr|April|May|May|Jun|June|Jul|July|Aug|August|Sep|September|Oct|October|Nov|November|Dec|December)|(?:3[01]|[12][0-9]|0?[1-9])[ \t]+(?:Jan|January|Feb|February|Mar|March|Apr|April|May|May|Jun|June|Jul|July|Aug|August|Sep|September|Oct|October|Nov|November|Dec|December)

Open in new window


This should cover you for data like:
123.10

Open in new window

where 23.10 would match without the addition to the pattern.
0
 

Author Closing Comment

by:novreisb
ID: 39890785
Yes, it was not only the following digits but also the precedent ones.  it works perfect!
 Thks Terry
 (ozo, thks, but Terry answer is more complete as you can understand)
0
 

Author Comment

by:novreisb
ID: 39890798
Sorry Terry, but the look behind not working! Can you try it?
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 35

Expert Comment

by:Terry Woods
ID: 39890818
It's only included for the dates with format:
123.10

If you have format:
123 October

Then you'll need a modified pattern. Is that the case you're talking about?

If not, then which case is failing?
0
 

Author Comment

by:novreisb
ID: 39890827
123.10 is failing! I removed the <  in the (?!<\d)  and it works apparently!
0
 
LVL 35

Expert Comment

by:Terry Woods
ID: 39890831
Apologies; I got the < and ! the wrong way around. It's corrected here:
(?<!\d)(?:3[01]|[12][0-9]|0?[1-9])[/.-](?:1[0-2]|0?[1-9])(?!\d)|(?:1[0-2]|0?[1-9])[/.-](?:Jan|January|Feb|February|Mar|March|Apr|April|May|May|Jun|June|Jul|July|Aug|August|Sep|September|Oct|October|Nov|November|Dec|December)|(?:3[01]|[12][0-9]|0?[1-9])[ \t]+(?:Jan|January|Feb|February|Mar|March|Apr|April|May|May|Jun|June|Jul|July|Aug|August|Sep|September|Oct|October|Nov|November|Dec|December)

Open in new window

0
 

Author Comment

by:novreisb
ID: 39890838
Excellent! Now is Ok! Have a good day that in Portugal is 3.00  AM and go sleep!
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Displaying an arrayList in a listView using the default adapter is rarely the best solution. To get full control of your display data, and to be able to refresh it after editing, requires the use of a custom adapter.
Ever notice how you can't use a new drive in Windows without having Windows assigning a Disk Signature?  Ever have a signature collision problem (especially with Virtual Machines?)  This article is intended to help you understand what's going on and…
This video Micro Tutorial explains how to clone a hard drive using a commercial software product for Windows systems called Casper from Future Systems Solutions (FSS). Cloning makes an exact, complete copy of one hard disk drive (HDD) onto another d…
With the advent of Windows 10, Microsoft is pushing a Get Windows 10 icon into the notification area (system tray) of qualifying computers. There are many reasons for wanting to remove this icon. This two-part Experts Exchange video Micro Tutorial s…

895 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

14 Experts available now in Live!

Get 1:1 Help Now