Page 1 of 1
Square brackets [] in string
Posted: Mon Jun 09, 2008 6:11 am
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
Re: Square brackets [] in string
Posted: Mon Jun 09, 2008 6:26 am
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.
Re: Square brackets [] in string
Posted: Mon Jun 09, 2008 6:37 am
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
Re: Square brackets [] in string
Posted: Mon Jun 09, 2008 6:42 am
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.
Re: Square brackets [] in string
Posted: Mon Jun 09, 2008 8:18 am
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
Re: Square brackets [] in string
Posted: Mon Jun 09, 2008 8:29 am
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?
Re: Square brackets [] in string
Posted: Mon Jun 09, 2008 8:33 am
by Liverpool
Sorry No,
Re: Square brackets [] in string
Posted: Mon Jun 09, 2008 9:07 am
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.