I have the following string
我有以下字符串
[custID] = 'A99999999'
[custID] ='A99999999'
I am trying the following to remove the square brackets and the single quotes
我正在尝试以下方法删除方括号和单引号
Regex.Replace(sql, "/[\[\]']+/g", " ")
but that's not working. I keep getting the same results
但那不起作用。我一直得到相同的结果
Note: sql is a variable holding the string above.
注意:sql是一个包含上面字符串的变量。
I want the result to be
我想要结果
custID = A99999999
custID = A99999999
5 个解决方案
#1
14
\[
doesn't do what you think it does. Nor is Regex
the same as in Perl. Try this instead:
\ [不会做你认为它做的事情。正则表达式与Perl相同。试试这个:
Regex.Replace(sql, @"[\[\]']+", "");
#2
1
Is this what you're looking for? ['\\/]
这是你在找什么? ['\\ /]
That should match any single character that is a slash or single quote.
这应该匹配任何斜杠或单引号的单个字符。
#3
1
I think you're looking for Regex.Replace(sql, @"[\[\]']", " ")
; the @ introduces a string where you need no escapes, and Regex.Replace
replaces all matches, so no need for the g
flag - your regex syntax isn't supported here, I think.
我想你正在寻找Regex.Replace(sql,@“[\ [\]']”,“”); @引入了一个不需要转义的字符串,Regex.Replace替换了所有匹配,因此不需要g标志 - 我认为这里不支持你的正则表达式语法。
#4
1
You can use this:
你可以用这个:
Regex.Replace(sql, "[][']", "")
If you're wondering how does that work, ]
right after [
isn't treated as closing, but as literal character.
如果你想知道它是如何工作的,那就在[不被视为结束,而是作为字面字符。
#5
0
You can use string split and join like below:
您可以使用字符串拆分和连接,如下所示:
string sql = "[custID] = 'A99999999'";
var correctedString = string.Join("",sql.Split(new char[] {'[', ']', '\''}));
Console.Write(correctedString);
If you want to use regex use [\[\]']
to replace those.
如果要使用正则表达式,请使用[\ [\]']替换它们。
#1
14
\[
doesn't do what you think it does. Nor is Regex
the same as in Perl. Try this instead:
\ [不会做你认为它做的事情。正则表达式与Perl相同。试试这个:
Regex.Replace(sql, @"[\[\]']+", "");
#2
1
Is this what you're looking for? ['\\/]
这是你在找什么? ['\\ /]
That should match any single character that is a slash or single quote.
这应该匹配任何斜杠或单引号的单个字符。
#3
1
I think you're looking for Regex.Replace(sql, @"[\[\]']", " ")
; the @ introduces a string where you need no escapes, and Regex.Replace
replaces all matches, so no need for the g
flag - your regex syntax isn't supported here, I think.
我想你正在寻找Regex.Replace(sql,@“[\ [\]']”,“”); @引入了一个不需要转义的字符串,Regex.Replace替换了所有匹配,因此不需要g标志 - 我认为这里不支持你的正则表达式语法。
#4
1
You can use this:
你可以用这个:
Regex.Replace(sql, "[][']", "")
If you're wondering how does that work, ]
right after [
isn't treated as closing, but as literal character.
如果你想知道它是如何工作的,那就在[不被视为结束,而是作为字面字符。
#5
0
You can use string split and join like below:
您可以使用字符串拆分和连接,如下所示:
string sql = "[custID] = 'A99999999'";
var correctedString = string.Join("",sql.Split(new char[] {'[', ']', '\''}));
Console.Write(correctedString);
If you want to use regex use [\[\]']
to replace those.
如果要使用正则表达式,请使用[\ [\]']替换它们。