Solved

c# Regex Valid Dates (without following digits )

Posted on 2014-02-26
8
505 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
Comment Utility
(?:1[0-2]|0?[1-9])$
0
 
LVL 35

Accepted Solution

by:
Terry Woods earned 500 total points
Comment Utility
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
Comment Utility
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
Comment Utility
Sorry Terry, but the look behind not working! Can you try it?
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 35

Expert Comment

by:Terry Woods
Comment Utility
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
Comment Utility
123.10 is failing! I removed the <  in the (?!<\d)  and it works apparently!
0
 
LVL 35

Expert Comment

by:Terry Woods
Comment Utility
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
Comment Utility
Excellent! Now is Ok! Have a good day that in Portugal is 3.00  AM and go sleep!
0

Featured Post

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Visual xHarbour 1 74
Reading variable length EBCDIC in SAS 9 74
changePi Challenge 15 75
Permutation and Combination 9 45
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
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 video, we discuss why the need for additional vertical screen space has become more important in recent years, namely, due to the transition in the marketplace of 4x3 computer screens to 16x9 and 16x10 screens (so-called widescreen format). …
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …

762 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

12 Experts available now in Live!

Get 1:1 Help Now