Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Like Operator

Status
Not open for further replies.

xhat

Technical User
May 18, 2009
25
US
Working in Access 2000 on XP box:

VBA code uses Select Case structure to search a text file for a string pattern...

Code:
Do Until StrComp(PSrsc.Fields("Field1"), "NEW HAND", vbBinaryCompare) = 0
    bnDeadHand = False
    Select Case True
        Case PSrsc.Fields("Field1") Like "*collected*"
            bnDeadHand = True
        Case PSrsc.Fields("Field1") Like "*[AKQJT#][scdh][ ][AKQJT#][scdh]*"
            strPlayer = Replace(Left(PSrsc.Fields("Field1"), 6), " ", "")
            strCards = CopyInString("[", PSrsc.Fields("Field1"), "]")
                PSUpdateStr = "UPDATE Showdowns SET " & strPlayer & " = '" & strCards & "' WHERE Hand_Num = '" & strHandNum & "'"
                DoCmd.SetWarnings False
                DoCmd.RunSQL PSUpdateStr
                DoCmd.SetWarnings True
    End Select

Certain lines within the text file have the form of "[some player] showed [As Kd] and won....." where '[As Kd]' means the player was holding the Ace of spades and the King of diamonds. The second Case criteria above ought to evaluate to True when these certain lines are parsed, however, it is not working.

In plain language, what I think I've written in the second Case criteria is this: If a line contains a pattern like (any one of the following letters) A-K-Q-J-T or a number 0-9 followed by (any one of the following letters) s-c-d-h, followed by a space, then (any one of the following letters) A-K-Q-J-T or a number 0-9 followed by (any one of the following letters) s-c-d-h, evaluate to True and process the next line of code. The two asterisks at the front and end of the pattern simply say any characters in any quantity can come before and after the pattern.

Set watches to step through the code by line, and when I hit the text line that I think will evaluate to True, it doesn't.

My sneaking hunch is that VBA does a terrible job of handling regex, which is basically what the Like operator is doing, but I'm hopeful there's some little trick someone out there knows that will make this work. Or maybe a more elegant solution?

Thanks

"There is no spoon..." - anonymous enlightened child
 
Never mind. I figured out the issue. Replaced the '#' with 2-9, so that the new search pattern looks like... [AKQJT2-9]. Why this works, I have no idea, since in theory, using the old pattern with the pound should have produced results in the case of [AKQJT] holdings, but apparently having the # inside the brackets is an issue.

"There is no spoon..." - anonymous enlightened child
 
Not sure if I understand the cause of the problem, but maybe a possible work around.

I tested the four possible cases
Like "*[AKQJT][scdh][ ][AKQJT][scdh]*"
Like "*[#][scdh][ ][AKQJT][scdh]*"
Like "*[AKQJT][scdh][ ][#][scdh]*"
Like "*[#][scdh][ ][#][scdh]*"

and only the pattern with only letters would resolve to true. Here is the test
Code:
Public Function checkMatch(ByVal str As String) As Boolean

 'Dim i As Integer
 'For i = 1 To 9
 '  str = Replace(str, i, "A")
 'Next i
  
  checkMatch = str Like "*[AKQJT][scdh][ ][AKQJT][scdh]*"
  If checkMatch Then Exit Function
  checkMatch = str Like "*[#][scdh][ ][AKQJT][scdh]*"
  If checkMatch Then Exit Function
  checkMatch = str Like "*[AKQJT][scdh][ ][#][scdh]*"
  If checkMatch Then Exit Function
  checkMatch = str Like "*[#][scdh][ ][#][scdh]*"
End Function

Public Sub testCheck()
 Dim str As String
 str = "zzzKs Qc123"
 Debug.Print str & " " & checkMatch(str)
  str = "zzz9s Qc123"
 Debug.Print str & " " & checkMatch(str)
  str = "zzzAs 9c123"
 Debug.Print str & " " & checkMatch(str)
   str = "zzz9s 9c123"
 Debug.Print str & " " & checkMatch(str)
End Sub

But I could cheat and then replace a number with "A" and this works.
So I think you could in your you could just call "checkMatch" and pass the string

 
That is an easier solution, but we identified the same issue with the #.
 
# stands for a digit OUTSIDE the [] context ...
 
Thank you both for looking into this. My thoughts regarding the pound, at first, was that the pattern would match on any of the alpha chars listed, or any number. As I read more online, I began to suspect that the pound should fall outside the brackets to work properly, but that made me start to wonder why certain hands were being processed, and others weren't. For example, [As Td] was evaluating true, but [Ac 3h] was not evaluating true. I realize now that the entire[/] expression had to evaluate to true, so in the case of [Ac 3h], the pattern matching was failing on the '3' thus not producing a True evaluation. Thanks again, guys, for looking into this.

"There is no spoon..." - anonymous enlightened child
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top