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:" & "$")Thanks
Moderator: General Moderators
Code: Select all
If Regex.IsMatch(listbox1, "^" & "IDS=[TEST]#SAMEAS:" & "$")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 ']':Liverpool wrote:Hi
I am searching for a certin string "IDS=[TEST]#SAMEAS:".
The expression i am using is: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.Code: Select all
If Regex.IsMatch(listbox1, "^" & "IDS=[TEST]#SAMEAS:" & "$")
Thanks
Code: Select all
If Regex.IsMatch(listbox1, "^" & "IDS=\[TEST\]#SAMEAS:" & "$")Code: Select all
If Regex.IsMatch(listbox1, "^\Q" & "IDS=[TEST]#SAMEAS:" & "\E$")There is one thing you could try: perhaps the \ needs to be escaped within the string literal themselves:Liverpool wrote:Thanks for the suggestions, it seems VB.NET does not support the second option
Code: Select all
If Regex.IsMatch(listbox1, "^\\Q" & "IDS=[TEST]#SAMEAS:" & "\\E$")No problem, glad to be of help.Liverpool wrote:but the first option works great.
Thanks for the help
I take it that the suggestion from my previous reply: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
Code: Select all
If Regex.IsMatch(listbox1, "^\\Q" & "IDS=[TEST]#SAMEAS:" & "\\E$")Okay.Liverpool wrote:Sorry No,
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
}