Re: Regex question




"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.

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:

That was what I was looking for.

Regex dateRegex = New Regex(@"\d{1,2}/\d{1,2}/\d{2,4}");

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.
What I was planning to do - just wasn't sure of the regex.

Thanks,

Tom




.



Relevant Pages

  • Re: Regex question
    ... structure of the date you're trying to extract. ... For example, in Regex you can ... pattern that will ensure a valid date within the range allowed by T-SQL ... valid date from a string. ...
    (microsoft.public.dotnet.languages.csharp)
  • RE: regular expression question
    ... I am a newbie to regular expressions and want to extract a number from the ... end of a string within an HTML document. ... A really nice free regex editor is called expresso. ...
    (microsoft.public.dotnet.framework.aspnet)
  • Re: Regex question
    ... If you want to pull a valid date from a string, then you have to write a pattern that defines the lexical structure of the date you're trying to extract. ... What you're trying to do is simple by Regex standards, but still requires a lot more specificity than "a digit or a slash". ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Convert String to COlor
    ... It is possible with that string to extract it using a regex, ... indexof or any combination of that. ...
    (microsoft.public.dotnet.languages.vb)
  • Re: Print# Questions
    ... when I extract it from the file I print #1 it to? ... position and get rid of that space. ... a string before printing it to the file. ...
    (microsoft.public.vb.general.discussion)