Re: Regex question




"Peter Duniho" <NpOeStPeAdM@xxxxxxxxxxxxxxxx> wrote in message
news:op.umftqmxa8jd0ej@xxxxxxxxxxxxxxxxxxxxxxx
On Fri, 19 Dec 2008 17:26:41 -0800, tshad <tfs@xxxxxxxxxxxxxx> wrote:

What is really strange is that 05/07/08(-4%) is converting to:

05/07/084

Why is that strange? Your regex basically removes anything that's not a
digit or a '/' character.

Convert.ToDateTime is change this to: 05/07/0084 12:00:00 AM????

Well, what did you expect it to do with the year "084"?

I know that. I didn't know the range would be that much different from SQL
but it obviously is.


But Sql says it is invalid and gives me an error:

Message: SqlDateTime overflow. Must be between 1/1/1753 12:00:00 AM
and
12/31/9999 11:59:59 PM.

I don't know much about SQL, but it looks like the range of valid dates in
SQL does not include the year 84. Not that I see any reason you'd _want_
it to accept the year 84, given the original string you started with.

I don't want it to accept this date. The problem is that my regex only gets
rid of everything that is not a number and a "/". This works for about 99%
of my dates (out of about 200,000 at the moment).

But I would like to figure out a better regex option.

It turns out all of these examples die in SQL but pass in VS.

What does that mean? It's clear why the result of your conversion isn't
accepted by SQL. But what does it mean for something to "pass in VS"?

I mean that it considers it a valid date - which it is but not really THE
valid date.


The code I use is:

public static DateTime? CleanDates(string valueIn)
{
DateTime? datValue = null;
string strValue;

strValue = Regex.Replace(valueIn, @"[^\d/]", "");
try
{
datValue = Convert.ToDateTime(strValue);
}
catch
{
datValue = null;
}
return datValue;
}

I would have assumed it would take the Catch, but doesn't.

Why would you have assumed that the execution would wind up in the "catch"
clause? You had a successful conversion from the string to a DateTime
value.

I agree and that is my problem. It passes (valid) in VS and then dies in
SQL. But SQL is only dropping it because it is outside the date range.
That doesn't necessarily making it correct. Being a valid date doesn't make
it the correct date. Which is my problem.

As far as the original question goes, you're not being specific enough
about what it is you really want to do. It seems to me that if you want
to actually extract dates of the form "mm/dd/yy" or "mm/dd/yyyy", you
should write a regex pattern that matches _that_, rather than trying to
remove all the characters from the string that aren't digits or '/'
characters.

I agree. Which was what I was asking. I just wanted to see if anyone knew
of one off hand.

From your examples, it appears that you always have two-digit months and
dates, but may have two- or four-digit years. So, perhaps rather than
using the Replace() method, you could use the Match() method, with a
string like this: @"\d\d/\d\d/\d\d(\d\d)?" Keeping in mind that I'm no
regex expert, and might have something not quite right in that one. But
hopefully you get the idea; that is, that there is in fact a specific
pattern that you're looking for, so matching against that pattern is
likely to be more fruitful than trying to exclude specific characters.

I agree. I am actually looking for 1 or 2 characters in the days and months
and 2 or 4 in the last. Exactly that pattern. Anything else I want to get
rid of.

Thanks,

Tom

Pete


.



Relevant Pages

  • Re: Finding a pattern in a stream?
    ... You could concatenate the two reads together, or match as much as the ... pattern in the first read, remember how much you did match, then match the ... What if my characters ARE spread ... >> Like operator is a simplified form of RegEx. ...
    (microsoft.public.dotnet.languages.vb)
  • Re: Cant get this regular expression figured out
    ... figure out how the regular expression should look to match any ... characters other than decimal numbers and '-'. ... correctly, I'd use the pattern: ... That regex might tell the OP whether what was typed was right or not. ...
    (comp.lang.tcl)
  • Re: [PHP] not sure why regex is doing this
    ... The regex does continue trying to make matches, but the point at which it ... characters match the pattern, but those characters are not consumed. ... So that regex is equivalent to "match a pattern starting with a comma ... negative lookahead... ...
    (php.general)
  • SQL query help (return only alphanumeric records)
    ... I need to write a sql to give records when data in Type field is ... alphanumeric i.e. mix of characters and numbers (no pattern). ...
    (microsoft.public.sqlserver.programming)
  • Re: Using part of a field
    ... See the article "Finding and replacing characters using wildcards" at: ... AS400 SQL may have a different way of doing ... called i.dsn, containing the followiing text: ... sort of thing in a query. ...
    (microsoft.public.word.mailmerge.fields)

Quantcast