473,758 Members | 5,909 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 6007
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
3324
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
2114
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
6491
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
19849
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
1625
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
8844
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
3196
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
2832
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
8277
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
9489
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9298
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10072
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
9906
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
9885
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,...
1
7286
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
1
3829
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
3
3399
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2698
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.