By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,247 Members | 1,517 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,247 IT Pros & Developers. It's quick & easy.

LIKE operator fails with multiple occurance of string in pattern?

P: n/a
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.
Nov 21 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
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/de...geElements.asp

Hope this helps
Jay
"Ed Brown" <eb****@computer-systems.com> wrote in message
news:e7*************@TK2MSFTNGP09.phx.gbl...
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.

Nov 21 '05 #2

P: n/a
Thanks Jay, using the RegEx class did the trick.

"Jay B. Harlow [MVP - Outlook]" <Ja************@msn.com> wrote in message
news:up**************@TK2MSFTNGP15.phx.gbl...
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/de...geElements.asp

Hope this helps
Jay
"Ed Brown" <eb****@computer-systems.com> wrote in message
news:e7*************@TK2MSFTNGP09.phx.gbl...
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.


Nov 21 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.