Al
Mon Dec 17 22:04:21 CST 2007
"Dr J R Stockton" <jrs@merlyn.demon.co.uk> wrote in message
news:BdGOiFYWquWHFw3e@invalid.uk.co.demon.merlyn.invalid...
> In microsoft.public.scripting.vbscript message <#a4UxlNOIHA.1212@TK2MSFT
> NGP05.phx.gbl>, Fri, 7 Dec 2007 14:59:39, Tony J <tony@creations.be>
> posted:
>>
>>My solution:
>>
>>Function IsDateRegExp(dDate)
>> Dim bCheck
>> '--- Set object
>> Set RegularExpressionObject = New regexp
>> '--- Patern and options
>> RegularExpressionObject.Pattern = "^(0[1-9]|[12][0-9]|3[01])[-
>>/.](0[1-9]|1[012])[- /.](19|20)\d\d$"
>> '--- dd/mm/yyyy (0[1-9]|[12][0-9]|3[01])[- /.](0[1-9]|1[012])[-
>>/.](19|20)\d\d
>> '--- mm/dd/yyyy (0[1-9]|1[012])[- /.](0[1-9]|[12][0-9]|3[01])[-
>>/.](19|20)\d\d
>> '--- yyyy/mm/dd (19|20)\d\d[- /.](0[1-9]|1[012])[-
>>/.](0[1-9]|[12][0-9]|3[01])
>> RegularExpressionObject.Global = False
>> '--- Test date
>> IF RegularExpressionObject.Test(dDate) and IsDate(dDate) THEN '---
>>IsDate is needed for check existence of the date
>> IsDateRegExp = True
>> ELSE
>> IsDateRegExp = False
>> END IF
>> '--- Kill Object
>> Set RegularExpressionObject = Nothing
>>END FUNCTION
>
> Near end :
> if X then
> A = true
> else
> A = false
> is always unnecessary - use just A = X.
This is only really equivalent when X contains an actual boolean value (i.e.
true or false), which is, of course, true in the example of interest. if X
will consider a non-zero numeric value of X as true, so the above
if-then-else code would result in A being set to true if X were non-zero or
to false if X had a zero value. Using A = X as you suggest above would
result in A being assigned the numeric value of X. Of course, if A is then
used in a simple boolean expression, no problem. But watch out when things
get a little less straightforward.
InStr is a good example of a function that is sometimes assumed to return a
boolean result of true if the substring is found or false if it is not. But
if one assumes that the result of InStr is *exactly* the same as if it had
been coded to mean IsInStr...
string1 = "abcdefg"
hasB = inStr(string1,"b")
hasD = inStr(string1,"d")
if hasB then
wscript.echo "contains B"
else
wscript.echo "has no B"
end if
if hasD then
wscript.echo "contains D"
else
wscript.echo "has no D"
end if
if hasB and hasD then
wscript.echo "contains B & D"
else
wscript.echo "does not contain B & D"
end if
But I agree that if the expression being tested can only be boolean, the
direct assignment makes far more sense. I seem to recall having a somewhat
lengthy discussion of this a while back and was surprised at how many people
seemed afraid to do it this way...
/Al
> Study Booleans until you understand why.
>
> As you know, isDate is well-nigh useless, except for those who want
> their code to run without confessed error but do not care whether the
> answer is good.
>
> IMHO, one rarely needs to check the validity of a date FULL STOP (period
> in USA). One generally needs to read its value and know whether that is
> good.
>
> Numeric-type checking with a RegExp is generally lengthy, complex, and
> error-prone. Your limit to 19xx & 20xx can easily be done with a
> numeric check (and will lead to eventual system failure). And I think
> you don't check for normal month-length, let alone the effects of Leap
> Years.
>
> The best move, therefore, is to check the input by RegExp for having an
> acceptable pattern of digits and separators, including no more than two
> Date digits, then to use DateSerial to get a CDate, then to check the
> Month part for matching the input.
>
> See REcheckDate in <URL:
http://www.merlyn.demon.co.uk/vb-date1.htm#SaR>,
> noting that it can be simplified for any specific date field order and
> exact pattern. It gives a full Gregorian check, including all month-
> length. Note that, as is, it is liberal about field separation, not
> even requiring that the separators be the same. The latter could easily
> be achieved if VBS RegExps understand back references.
>
> --
> (c) John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v6.05 IE
> 6.
> Web <URL:
http://www.merlyn.demon.co.uk/> - w. FAQish topics, links,
> acronyms
> PAS EXE etc : <URL:
http://www.merlyn.demon.co.uk/programs/> - see
> 00index.htm
> Dates - miscdate.htm moredate.htm js-dates.htm pas-time.htm critdate.htm
> etc.