.NETGURU
LIKE operator fails with multiple occurance of string in pattern?
Messages   Related Types
This message was discovered on microsoft.public.dotnet.languages.vb.
Responses highlighted in red are from those people who are likely to be able to contribute good, authoratitive information to this discussion. They include Microsoft employees, MVP's and others who IMHO contribute well to these kinds of discussions.
Post a new message to this list...

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.

Reply to this message...
 
    
Jay B. Harlow [MVP - Outlook] (VIP)
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/default.asp?url=/library/en-us/cpgenref/html/cpconRegularExpressionsLanguageElements.asp

Hope this helps
Jay

"Ed Brown" <Click here to reveal e-mail address> wrote in message
news:Click here to reveal e-mail address...
[Original message clipped]

Reply to this message...
 
    
Ed Brown
Thanks Jay, using the RegEx class did the trick.

"Jay B. Harlow [MVP - Outlook]" <Click here to reveal e-mail address> wrote in message
news:Click here to reveal e-mail address...
[Original message clipped]

Reply to this message...
 
 
System.EventArgs
System.Object
System.Text.RegularExpressions.Regex
System.Text.RegularExpressions.RegexOptions
System.Windows.Forms.MessageBox




ExamGuru IT Solutions - .Net Guru is owned and operated by ExamGuru, Inc., the man behind .Net Guru. If you're in the market for bespoke software or software consultancy, why not get him and his highly trained team to help? - www.examguru.net/ITCertification
Ad


Need Dot Net Interview Questions?
Ask ExamGuru, Inc. for advice and help on Passing .Net Interviews
.Net Projects
Best-of-breed application framework for .NET projects, developed by ExamGuru, Inc. and ExamGuru IT
Free .net Help
Commission ExamGuru, Inc. and his team for your next bespoke software project
FogBUGZ
The only bug tracking system carefully crafted with one goal in mind: helping teams create great software.
Awesome Tools
If you don't know about these, you're missing out... IT Certification Questions
IT Interview Questions
Free Oracle 10g Training
MCSE Boortcamp
Cisco Study Guides
Cheap Study Guides
Exact Questions
Dot Net Interview Questions
Oracle OCP
Cheap Travel
Designer Perfumes - Wholesale Prices
Free Programming Tutorials
 
ExamGuru IT Solutions - .Net Guru is owned and operated by ExamGuru, Inc., the man behind .Net Guru. If you're in the market for bespoke software or software consultancy, why not get him and his highly trained team to help? - www.examguru.net/ITCertification
 Copyright © ExamGuru, Inc. 2001-2006
Contact Us - Terms of Use - Privacy Policy - www.dot-net-guru.com - www.examguru.net - www.oraclesource.net - www.itinterviews.net - www.examguru.net/ITCertification