ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   Test for exact match in string (https://www.excelbanter.com/excel-programming/307801-test-exact-match-string.html)

Jeremy Gollehon[_2_]

Test for exact match in string
 
OK, this one is really getting to me. Without using Excel specific
functions, how can I test if I've found the exact match of a word or phrase?
I've came up with the code below, which works great in most cases (Test1 and
Test2). My problem is I could be testing html source code (Test3). My head
hurts, so I post here in hopes of some help. Thanks!


Sub tester()
Dim vSplit As Variant
Dim bExactMatch As Boolean
Dim sTest1 As Variant
Dim sTest2 As Variant
Dim sTest3 As Variant


sTest1 = "this is a test, here's the stuff you wanted. "
vSplit = Split(sTest1, " here's ")
bExactMatch = UBound(vSplit) < 0
Debug.Print "Test1: " & bExactMatch

sTest2 = "this is a test, there's the stuff you wanted. "
vSplit = Split(sTest2, " here's ")
bExactMatch = UBound(vSplit) < 0
Debug.Print "Test2: " & bExactMatch

sTest3 = "<bhere's</b the stuff you wanted. "
vSplit = Split(sTest3, " here's ")
bExactMatch = UBound(vSplit) < 0
Debug.Print "Test3: " & bExactMatch

End Sub



Jeremy Gollehon[_2_]

Test for exact match in string
 
OK, so after writing this question I thought of at least one answer, replace
the tag identifiers with spaces. This might not be the most elegant
solution, but it works.

If you've got something better, I'd love to see it. Thanks.

Sub tester()

sTest3 = "<bhere's</b the stuff you wanted. "
sTest3 = Replace(sTest3, "", " ")
sTest3 = Replace(sTest3, "<", " ")
vSplit = Split(sTest3, " here's ")
bExactMatch = UBound(vSplit) < 0
Debug.Print "Test3: " & bExactMatch

End Sub

<snip



RB Smissaert

Test for exact match in string
 
I might be overlooking something, but what is wrong with using the Instr
function?

RBS


"Jeremy Gollehon" wrote in message
...
OK, this one is really getting to me. Without using Excel specific
functions, how can I test if I've found the exact match of a word or

phrase?
I've came up with the code below, which works great in most cases (Test1

and
Test2). My problem is I could be testing html source code (Test3). My

head
hurts, so I post here in hopes of some help. Thanks!


Sub tester()
Dim vSplit As Variant
Dim bExactMatch As Boolean
Dim sTest1 As Variant
Dim sTest2 As Variant
Dim sTest3 As Variant


sTest1 = "this is a test, here's the stuff you wanted. "
vSplit = Split(sTest1, " here's ")
bExactMatch = UBound(vSplit) < 0
Debug.Print "Test1: " & bExactMatch

sTest2 = "this is a test, there's the stuff you wanted. "
vSplit = Split(sTest2, " here's ")
bExactMatch = UBound(vSplit) < 0
Debug.Print "Test2: " & bExactMatch

sTest3 = "<bhere's</b the stuff you wanted. "
vSplit = Split(sTest3, " here's ")
bExactMatch = UBound(vSplit) < 0
Debug.Print "Test3: " & bExactMatch

End Sub




Jeremy Gollehon[_2_]

Test for exact match in string
 
I don't know. I think I've gone crazy. Time to stop thinking.
I believe the following function will work in all cases as long as I replace
the undesired items that can be surrounding the LookFor words (punctuation
and html tag indicators) with spaces before calling it.
Thanks RB.

Function StrExactMatch(sLookIn As String, sLookFor As String) As Boolean
'- Add padding to sLookin in case sLookFor is found
' at the very beginning or very end of the string.
'- Add padding to sLookFor to be sure we're getting an exact match
StrExactMatch = InStr(" " & sLookIn & " ", " " & sLookFor & " ") 0
End Function


RB Smissaert wrote:
I might be overlooking something, but what is wrong with using the
Instr function?

RBS


"Jeremy Gollehon" wrote in message
...
OK, this one is really getting to me. Without using Excel specific
functions, how can I test if I've found the exact match of a word or
phrase? I've came up with the code below, which works great in most
cases (Test1 and Test2). My problem is I could be testing html
source code (Test3). My head hurts, so I post here in hopes of some
help. Thanks!


Sub tester()
Dim vSplit As Variant
Dim bExactMatch As Boolean
Dim sTest1 As Variant
Dim sTest2 As Variant
Dim sTest3 As Variant


sTest1 = "this is a test, here's the stuff you wanted. "
vSplit = Split(sTest1, " here's ")
bExactMatch = UBound(vSplit) < 0
Debug.Print "Test1: " & bExactMatch

sTest2 = "this is a test, there's the stuff you wanted. "
vSplit = Split(sTest2, " here's ")
bExactMatch = UBound(vSplit) < 0
Debug.Print "Test2: " & bExactMatch

sTest3 = "<bhere's</b the stuff you wanted. "
vSplit = Split(sTest3, " here's ")
bExactMatch = UBound(vSplit) < 0
Debug.Print "Test3: " & bExactMatch

End Sub





All times are GMT +1. The time now is 05:58 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com