[AccessD] Find characters in one string that are in another: was RE: Treat string as array

Charlotte Foust cfoust at infostatsystems.com
Tue Mar 25 17:31:07 CDT 2008


Yep, I love em.  My boss prefers InStr as more "readable", so I don't
get to use them very often ... Even in dot net. :-<

Charlotte Foust 

-----Original Message-----
From: accessd-bounces at databaseadvisors.com
[mailto:accessd-bounces at databaseadvisors.com] On Behalf Of Michael Bahr
Sent: Tuesday, March 25, 2008 2:59 PM
To: Access Developers discussion and problem solving
Subject: Re: [AccessD] Find characters in one string that are in
another: was RE: Treat string as array

John here is a regular expression version of exactly want you specified;
a password with 1 special char and 1 digit.  There is no sting exploding
into arrays, no looping.  I have simplified the function so it is very
readable.

With all due respect you see regex's as ARCANE because you have not
learned to use regular expressions.  It is very powerful, flexible tool
and can do so much to help any programmer.  It is not that hard to do
once you understand the concepts.

BTW, you really should test for other chars not allowed like space(s),
etc.  I tested this in A2K3.

Enjoy, Mike...


Function testPW () As Boolean
   Dim regex As Object
   Dim pw as variant
   Dim regexMatch1 As Boolean, regexMatch2 As Boolean
   Dim regexBadMatch As Boolean
   Dim s1 As String, s2 As String

   s1 = "\~|\!|\@|\#|\$|\%|\^|\&|\*|\(|\)|\_|\+"
'   s1 = "[~!@#$%^&*()_+]" ' still is a class of chars
   s2 = " |\?|\<|\>|\{|\}|\:|\;"

   pw = "hello123World!"
'   pw = "hello1World+"
'   pw = "hel at lo23World"
'   pw = "hello1I%World"

   Set regex = CreateObject("VBScript.RegExp")
   regexMatch = True
   regex.Global = True
   regex.ignorecase = True

   ' test for illegal char
   regex.pattern = s2
   regexBadMatch = regex.test(pw)
   If (regexBadMatch) Then
      ' invalid password
      ' abort, exit, terminate, prompt for new password
      testPW = 0
   End If

   ' test for only 1 special char
   regex.pattern = s1
   regexMatch = regex.test(pw)

   ' test for only 1 digit
   regex.pattern = "\d"
   regexMatch = regex.test(pw)

   If (regexMatch1 And regexMatch2) Then
      ' got a valid password
      testPW = 1
   Else
      ' invalid password
      testPW = 0
   End IF

End Function






> ROTFLMAO.
>
> If you look back at my ORIGINAL post, there were no spaces specified.

> It is rather like the old "Whisper in the ear of the person on your 
> left and see what comes back in your right ear."  The more I try to 
> explain, the worse the results.
>
>>How can I turn a string into an array?  I need to iterate through the
> characters of a string.
>
>>For each char in str
>>	dosomething char
>>Next char
>
> That seems like a simple enough question.
>
>>How can I turn a string into an array?  I need to iterate through the
> characters of a string.
>
> FOR THE RECORD... I UNDERSTAND MID() QUITE WELL.
>
> Also for the record, there were precisely TWO suggestions that 
> indicated that the reader had any concept of what I was asking.  One 
> from Gustav and the other from William.
>
> However I would like to thank everyone for the responses.
>
> Further for the record, I have already implemented the mid() scenario 
> (days
> ago) and solved the immediate problem which was:
>
> I was trying to search for characters in a password that are also in a

> "must contain" string.
>
> "The password must contain one 'special character' ~!@#$%^&*()_+ and 
> the password must contain one 'number' 1234567890.
>
> Given a password "The thread that went south", how do I determine if 
> it contains any special characters or numbers.
>
> Of course the obvious (and much suggested) solution was mid(), which 
> in the end I just used.  It was fast and easy and ugly.
>
> I was hoping for a more elegant and readable solution
>
> 	MyArr = SomeConvFunction(strPassword)
> 	for each chr in MyArray
> 		if chr instr(cstrSpecialChars) then
> 			We're done so get out.
> 		endif
> 	next chr
>
> Compare the readability of that to:
>
> Function mStrContainsChars(strToTest As String, strCharsTestedAgainst 
> As
> String) As Boolean
> Dim intPtr As Integer
> Dim strChar As Variant
>
>     For intPtr = 1 To Len(strToTest)
>         strChar = Mid(strToTest, intPtr, 1)
>         If InStr(strCharsTestedAgainst, strChar) Then
>             mStrContainsChars = True
>             Exit Function
>         End If
>     Next intPtr
> End Function
>
> I did get a pair of possible solutions (which I have not tested yet, 
> given that I already coded the mid() solution.
>
> Hey Gustav... "how do I ..."
>
> Pass it on.  ;-)
>
> John W. Colby
> Colby Consulting
> www.ColbyConsulting.com
> -----Original Message-----
> From: accessd-bounces at databaseadvisors.com
> [mailto:accessd-bounces at databaseadvisors.com] On Behalf Of Gustav 
> Brock
> Sent: Tuesday, March 25, 2008 3:42 AM
> To: accessd at databaseadvisors.com
> Subject: Re: [AccessD] Treat string as array
>
> Hi A.D.
>
> Very creative solution!
>
> For how long time will JC chew on this dry stick? Next step is 
> probably that the spaces he specified originally which should not be 
> present should be present anyway or perhaps replaced by a tab or a 
> white space! Lots of options out there ...
>
> /gustav
>
>>>> adtp at airtelmail.in 25-03-2008 08:06 >>>
>     A very concise version of Function Fn_ConvStringToArray(), using 
> only a single statement, is placed below. It returns an array, so that

> individual characters of input string get placed in individual 
> elements of the array.
>
>     Note - In fact, right side of assignment statement (A) below, can 
> be used directly, as it is made up purely of access built-in
functions.
> Fn_ConvStringToArray() acts merely as a wrapper function, for sake of 
> convenience.
>
> A.D.Tejpal
> ------------
>
> ' Code to be placed in VBA module
> '================================
> Function Fn_ConvStringToArray(StrInput _
>                                 As String) As Variant
>     Fn_ConvStringToArray = _
>                 Split(Format(StrInput, _
>                 Mid(Replace(String(Len(StrInput), _
>                 "~"), "~", "~@"), 2)), "~")                         '
(A)
> End Function
> '================================
>
> --
> AccessD mailing list
> AccessD at databaseadvisors.com
> http://databaseadvisors.com/mailman/listinfo/accessd
> Website: http://www.databaseadvisors.com
>


--
AccessD mailing list
AccessD at databaseadvisors.com
http://databaseadvisors.com/mailman/listinfo/accessd
Website: http://www.databaseadvisors.com




More information about the AccessD mailing list