Solved

How to use the like operator with special characters.

Posted on 2013-06-02
5
599 Views
Last Modified: 2013-06-02
I am trying to use the Like operator in VB.net to check to see if the following characters exist within a string:

"(", "[", "{", ")", "]", "}"

The problem is that VB.net uses brackets "[]" to group a series of characters, and brackets are one of the characters that I am looking for in my string.

What is the syntax that I must use to successfully check for the above group of character's in my string including brackets?


Thank you in advance for your time.
0
Comment
Question by:NevSoFly
  • 2
  • 2
5 Comments
 
LVL 38

Assisted Solution

by:Jim P.
Jim P. earned 200 total points
ID: 39214260
For square brackets try:
like "*" & Chr(91) & "*" & Chr(93) & "*" &

Open in new window


For squiggly brackets try:
like "*" & Chr(123) & "*" & Chr(125) & "*" &

Open in new window


Here's an ASCII table.
0
 
LVL 15

Expert Comment

by:ChloesDad
ID: 39214470
To match the special characters left bracket ([), question mark (?), number sign (#), and asterisk (*), enclose them in square brackets. The right bracket (]) cannot be used within a group to match itself, but it can be used outside a group as an individual character.

eg [[], [?]. [#] and [*]
0
 

Author Comment

by:NevSoFly
ID: 39214512
I read the Microsoft example too, but was having a hard time visualizing the syntax.  So if I included the right bracket would look something like [[({)}] []] ?
0
 
LVL 38

Expert Comment

by:Jim P.
ID: 39214523
When you do it using the Chr function it is handled as a literal value and not as a special character that has to be escaped.
0
 
LVL 15

Accepted Solution

by:
ChloesDad earned 300 total points
ID: 39214540
The right bracket is not allowed in this context

to search for either of the 6 brackets it would look like this I have added spaces for visual effect


* [   (  [[]  {  )  }  ] *

to then find a ] you would do a new LIKE *]*

so the overall logic would be

IF string LIKE "*[([[]{)}]*" OR string LIKE "*]*"
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: jpaulino
XML Literals are a great way to handle XML files and the community doesn’t use it as much as it should.  An XML Literal is like a String (http://msdn.microsoft.com/en-us/library/system.string.aspx) Literal, only instead of starting and ending with w…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…

860 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question