Solved

c# Regex Valid Dates (without following digits )

Posted on 2014-02-26
8
520 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
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

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.

 

Author Comment

by:novreisb
ID: 39890798
Sorry Terry, but the look behind not working! Can you try it?
0
 
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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

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

Whether you've completed a degree in computer sciences or you're a self-taught programmer, writing your first lines of code in the real world is always a challenge. Here are some of the most common pitfalls for new programmers.
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
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…
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…

856 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