473,722 Members | 2,346 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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(B yVal sender As System.Object, ByVal e As
System.EventArg s) 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 6003
Ed,
I cannot explain the Like operator as I don't use it very much, I use the
System.Test.Reg ularExpressions .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.Reg ularExpressions .Regex(".*TEST. *TEST.*")
Dim theString As String
theString = "1234 TEST 5432 TEST ABCD"
If theRegex.IsMatc h(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.Co mpiled option if the regex
is used a lot within my program.

Something like:

Imports System.Text.Reg ularExpressions

Static theRegex As New Regex(".*TEST.* TEST.*",
RegexOptions.Co mpiled)
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****@compute r-systems.com> wrote in message
news:e7******** *****@TK2MSFTNG P09.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(B yVal sender As System.Object, ByVal e As
System.EventArg s) 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******** ******@TK2MSFTN GP15.phx.gbl...
Ed,
I cannot explain the Like operator as I don't use it very much, I use the
System.Test.Reg ularExpressions .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.Reg ularExpressions .Regex(".*TEST. *TEST.*")
Dim theString As String
theString = "1234 TEST 5432 TEST ABCD"
If theRegex.IsMatc h(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.Co mpiled option if the
regex is used a lot within my program.

Something like:

Imports System.Text.Reg ularExpressions

Static theRegex As New Regex(".*TEST.* TEST.*",
RegexOptions.Co mpiled)
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****@compute r-systems.com> wrote in message
news:e7******** *****@TK2MSFTNG P09.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(B yVal sender As System.Object, ByVal e As
System.EventArg s) 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
3321
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) { var range = document.selection.createRange(); if (range.parentElement() == element) range.text = '<' + tag + '>' + range.text + '<\/' + tag + '>'; }
17
2110
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 available or not? If not how could we do this operation? Any stuffed replies or redirection will be appreciated. Thanks, Ganesh
4
6484
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 of the United States"; strchr will be successful if i am looking for the "U". It returns a
1
19840
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 regex and it supports very different characters and behaves different. So, I'm looking for an advise. I'd like to know if it is feasable or not, I mean, if dotnet Regex implementation could handle it.
4
1623
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 any time where more then a single ' (tic) occurs in succession, and replace it with a single ' (tic). Can this be done with a RegularExpression? Any example of how to do this?
2
8841
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 Dataset. The SELECT Command we are using is as follows DS.Tables(0).Select("FieldName = '" & input value & "')
2
3192
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 if called from within a different namespace and that other namespace has any operator<<() defined. That doesn't seem good.
1
2829
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 just one coloumn with string patterns.
6
8274
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 populated are lsited below but must be recorded to a new table. Gets info from Course occurance Table. Combo Box - Gets Information from Course Occurance Table. This table has all information regarding course information. Course ID: Course...
0
9383
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9236
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9154
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9088
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
5995
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4502
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4762
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3207
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2601
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.