LIKE operator fails with multiple occurance of string in pattern?

E

Ed Brown

I'm working on a VB.Net application that needs to do quite a bit of string
pattern matching, and am having problems using the "LIKE" operator to match
the same string twice in the pattern. For example, in the following code:

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
Dim theString As String
theString = "1234 TEST 5432 TEST ABCD"
If theString Like "*TEST*TEST*" Then
MessageBox.Show("Matches!")
Else
MessageBox.Show("No Match!")
End If
End Sub

I would expect the LIKE operator in this test to return true, since
theString matches the pattern "*TEST*TEST*", specifying zero-or-more
characters followed by "TEST" followed by zero-or-more characters followed
by "TEST" followed by zero-or-more characters. However, when I run the above
code, the LIKE operator returns false. If I change the pattern to
"*TEST*ABC*", the LIKE operator returns true. If I alter the pattern to
something like "*TEST*TEST A*", it again returns false. It seems that LIKE
won't match a pattern that contains the same string of characters twice. Can
anyone explain why the operator behaves this way?

Thanks in advance for any replies.
 
J

Jay B. Harlow [MVP - Outlook]

Ed,
I cannot explain the Like operator as I don't use it very much, I use the
System.Test.RegularExpressions.RegEx class instead. As the patterns
supported by RegEx far exceeds the patterns allowed in the Like operator.

Something like:

Dim theRegex As New
System.Text.RegularExpressions.Regex(".*TEST.*TEST.*")
Dim theString As String
theString = "1234 TEST 5432 TEST ABCD"
If theRegex.IsMatch(theString) Then
MessageBox.Show("Matches!")
Else
MessageBox.Show("No Match!")
End If

I will normally define my Regex variables as Shared within a class, or
Static within a routine, with the RegExOptions.Compiled option if the regex
is used a lot within my program.

Something like:

Imports System.Text.RegularExpressions

Static theRegex As New Regex(".*TEST.*TEST.*",
RegexOptions.Compiled)


The following site provides a good overview of regular expressions:

http://www.regular-expressions.info/

While this site provides the syntax specifically supported by .NET:

http://msdn.microsoft.com/library/d...l/cpconRegularExpressionsLanguageElements.asp

Hope this helps
Jay
 
E

Ed Brown

Thanks Jay, using the RegEx class did the trick.

Jay B. Harlow said:
Ed,
I cannot explain the Like operator as I don't use it very much, I use the
System.Test.RegularExpressions.RegEx class instead. As the patterns
supported by RegEx far exceeds the patterns allowed in the Like operator.

Something like:

Dim theRegex As New
System.Text.RegularExpressions.Regex(".*TEST.*TEST.*")
Dim theString As String
theString = "1234 TEST 5432 TEST ABCD"
If theRegex.IsMatch(theString) Then
MessageBox.Show("Matches!")
Else
MessageBox.Show("No Match!")
End If

I will normally define my Regex variables as Shared within a class, or
Static within a routine, with the RegExOptions.Compiled option if the
regex is used a lot within my program.

Something like:

Imports System.Text.RegularExpressions

Static theRegex As New Regex(".*TEST.*TEST.*",
RegexOptions.Compiled)


The following site provides a good overview of regular expressions:

http://www.regular-expressions.info/

While this site provides the syntax specifically supported by .NET:

http://msdn.microsoft.com/library/d...l/cpconRegularExpressionsLanguageElements.asp

Hope this helps
Jay
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top