Re: Regex question
- From: "tshad" <tfs@xxxxxxxxxxxxxx>
- Date: Thu, 25 Dec 2008 14:22:25 -0800
"Jeff Johnson" <i.get@xxxxxxxxxxx> wrote in message
news:%23%23J8PgFZJHA.4596@xxxxxxxxxxxxxxxxxxxxxxx
"tshad" <tfs@xxxxxxxxxxxxxx> wrote in message
news:OpJPJBkYJHA.5828@xxxxxxxxxxxxxxxxxxxxxxx
This is really a regex question.
I am wonding if anyone knows a good Regex expression that would pull a
valid date from a string.
I have used:
strValue = Regex.Replace(valueIn, @"[^\d/]", "");
which works most of the time.
But I have some cases where I have strings like:
05/07/08(-4%)
09/19/08 DOM 55
09/19/2008 DOM 53
FOR 09/15/08 -23
Stop using Replace to get rid of the stuff you don't want, because clearly
it's causing problems. Instead, examine all the possible inputs you might
get and then craft a regex to EXTRACT those parts. Then TEST what you've
extracted to see if it's a date. After all, 99/76/23 might fit the regex,
but it isn't a valid date.
And how would you suggest I do that??? These are just examples of some of
the inputs I am getting. I can't really get rid of any parts as I don't
know what will be where.
I have no control over what the user will enter in this case.
I need to be able to be able to find a date in the input. Using a variety
of possible (probable) date formats, I should be able to extract the date
from the input - if one exists.
That was what I was looking for.
If you are positive that the separator will always be a slash and that
you'll only have digits (not 10/Dec/2008), you might get away with this:
Regex dateRegex = New Regex(@"\d{1,2}/\d{1,2}/\d{2,4}");What I was planning to do - just wasn't sure of the regex.
Then you'll use the Match() method (or Matches) and see if you get
anything, and then Date.TryParse[Exact]() to see if it's a real date.
Thanks,
Tom
.
- Follow-Ups:
- Re: Regex question
- From: Josh Einstein
- Re: Regex question
- References:
- Regex question
- From: tshad
- Re: Regex question
- From: Jeff Johnson
- Regex question
- Prev by Date: Re: Regex question
- Next by Date: implicit conversion - what is guaranteed?
- Previous by thread: Re: Regex question
- Next by thread: Re: Regex question
- Index(es):
Relevant Pages
|