Square brackets [] in string

Any questions involving matching text strings to patterns - the pattern is called a "regular expression."

Moderator: General Moderators

Post Reply
Liverpool
Forum Newbie
Posts: 7
Joined: Thu Jun 05, 2008 6:49 am

Square brackets [] in string

Post by Liverpool »

Hi

I am searching for a certin string "IDS=[TEST]#SAMEAS:".

The expression i am using is:

Code: Select all

If Regex.IsMatch(listbox1, "^" & "IDS=[TEST]#SAMEAS:" & "$")
The search will not find the string because it contains square brackets [], if i remove the square brackets from the string then the search works, is there a way around this.

Thanks
User avatar
prometheuzz
Forum Regular
Posts: 779
Joined: Fri Apr 04, 2008 5:51 am

Re: Square brackets [] in string

Post by prometheuzz »

Liverpool wrote:Hi

I am searching for a certin string "IDS=[TEST]#SAMEAS:".

The expression i am using is:

Code: Select all

If Regex.IsMatch(listbox1, "^" & "IDS=[TEST]#SAMEAS:" & "$")
The search will not find the string because it contains square brackets [], if i remove the square brackets from the string then the search works, is there a way around this.

Thanks
The square brackets are interpreted as a character class: [TEST] will match 'T', 'E' or 'S'. If you need to match the string "[TEST]" you will need to escape the '[' and ']':

Code: Select all

If Regex.IsMatch(listbox1, "^" & "IDS=\[TEST\]#SAMEAS:" & "$")
You might also want to try this:

Code: Select all

If Regex.IsMatch(listbox1, "^\Q" & "IDS=[TEST]#SAMEAS:" & "\E$")
I'm not sure if the \Q ... \E is supported by VB.NET, but if it is, it will cause everything between it to match as it is, so no characters (like '[' and ']') are treated specially.
Liverpool
Forum Newbie
Posts: 7
Joined: Thu Jun 05, 2008 6:49 am

Re: Square brackets [] in string

Post by Liverpool »

Thanks for the suggestions, it seems VB.NET does not support the second option but the first option works great.

Thanks for the help
User avatar
prometheuzz
Forum Regular
Posts: 779
Joined: Fri Apr 04, 2008 5:51 am

Re: Square brackets [] in string

Post by prometheuzz »

Liverpool wrote:Thanks for the suggestions, it seems VB.NET does not support the second option
There is one thing you could try: perhaps the \ needs to be escaped within the string literal themselves:

Code: Select all

If Regex.IsMatch(listbox1, "^\\Q" & "IDS=[TEST]#SAMEAS:" & "\\E$")
but I am a complete VB illiterate, so I could be dead wrong!
;)

Liverpool wrote:but the first option works great.

Thanks for the help
No problem, glad to be of help.
Liverpool
Forum Newbie
Posts: 7
Joined: Thu Jun 05, 2008 6:49 am

Re: Square brackets [] in string

Post by Liverpool »

Although the first option works, the second option would have been ideal because the first option means that I have manually add the “\[\] to each string were as the second option means I would not have to edit the string.

Is there any other way of performing the same without having to edit the actual string.

Thanks
User avatar
prometheuzz
Forum Regular
Posts: 779
Joined: Fri Apr 04, 2008 5:51 am

Re: Square brackets [] in string

Post by prometheuzz »

Liverpool wrote:Although the first option works, the second option would have been ideal because the first option means that I have manually add the “\[\] to each string were as the second option means I would not have to edit the string.

Is there any other way of performing the same without having to edit the actual string.

Thanks
I take it that the suggestion from my previous reply:

Code: Select all

If Regex.IsMatch(listbox1, "^\\Q" & "IDS=[TEST]#SAMEAS:" & "\\E$")
(with the double escapes) didn't work?
Liverpool
Forum Newbie
Posts: 7
Joined: Thu Jun 05, 2008 6:49 am

Re: Square brackets [] in string

Post by Liverpool »

Sorry No,
User avatar
prometheuzz
Forum Regular
Posts: 779
Joined: Fri Apr 04, 2008 5:51 am

Re: Square brackets [] in string

Post by prometheuzz »

Liverpool wrote:Sorry No,
Okay.
I had a look at MS' website about regular expressions for VBScript:
http://msdn.microsoft.com/en-us/library ... g05_topic1

but did not find \Q...\E, or something similar. So, you'll have to escape the meta characters yourself, or write a function/method that takes a string as a parameter and return that string with all meta characters escaped. Like I said, I don't know VBScript, but here's how you could do it in Java:

Code: Select all

   public static String escape(String target) {
        String metaChars = "[]+*"; // add more
        StringBuffer buffer = new StringBuffer();
        for(char c: target.toCharArray()) { // iterate over all characters in the target String
            if(metaChars.indexOf(c) > -1) { // yes, we've found a meta character!
                buffer.append('\\'); // add a single \
            }
            buffer.append(c); // add the character
        }
        return buffer.toString(); // return the escaped String
    }
But even if you don't know Java, it shouldn't be too hard to figure out the logic and apply it in VBScript. I've also commented it a bit to explain how it works.

Good luck.
Post Reply