473,386 Members | 1,803 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

LIKE operator fails with multiple occurance of string in pattern?

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
2 5976
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

15
by: lawrence | last post by:
Is this the correct way to test for a method before I use it? createRange() is, I believe, an IE only method. function wrapSelectionInTag(selection, tag) { if (document.selection.createRange)...
17
by: sgane2001 | last post by:
Hi, I'm using pcre.c to provide regex support for my application software. I want to know how to do 'AND' operation in this. I didn't got any useful info from the net. Is this operator...
4
by: David Warner | last post by:
Greetings! In looking into some C coding, I am looking for the C function that will search for multiple occurances of a same character in a string. For Instance: char str = "We the people...
1
by: Craig Kenisston | last post by:
Hi, I need to write a function that should behave like the SQL's "like" operator on a list of words. I was wondering if I can use Regex directly to do this job. But I've been reading about...
4
by: Jim Heavey | last post by:
I am wanting to find any instance in a string where more then on occurance of a value occurs and replace it with a single occurance of the value. Specifically I want to search a text field and find...
2
by: Anandan | last post by:
Hi, In our Project we use Dataset to load the Grid with Values. We have some criteria to filter the values to be shown in the Grid. For that we used the SELECT command to filter the Same...
2
by: ryan_melville | last post by:
Hi, Should I put the operator<<() for my class (which is in a namespace) in the namespace or make it global? If I understand the lookup rules correctly: If I make it global, it may be hidden...
1
by: guest | last post by:
Hi, I want to use the like operator with multiple inputs but get an error This is wat i am doing Select x,y,z from tableA Group by x,y,z Having x like(Select * from tableB) table B has...
6
by: zuchowra | last post by:
Hi everyone. I need help. I have a combo box in my form that i want to populate multiple text boxes after you select your selection in the combo box. Here is my set up. The Fields that need to be...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.