Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove square brackets and single quotes regex not working

Tags:

c#

regex

vb.net

I have the following string

[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.

I want the result to be

custID = A99999999

like image 446
Saif Khan Avatar asked May 09 '11 19:05

Saif Khan


People also ask

How do you escape square brackets in regex?

If you want to remove the [ or the ] , use the expression: "\\[|\\]" . The two backslashes escape the square bracket and the pipe is an "or".

How do I enable square brackets in regex?

Just make sure ] is the first character (or in this case, first after the ^ . result2 = Regex. Replace(result2, "[^][A-Za-z0-9/.,>#:\s]", "");

How do you remove square brackets from string?

The easiest way to get rid of brackets is with a regular expression search using the Python sub() function from the re module. We can easily define a regular expression which will search for bracket characters, and then using the sub() function, we will replace them with an empty string.

What do the [] brackets mean in regular expressions?

By placing part of a regular expression inside round brackets or parentheses, you can group that part of the regular expression together. This allows you to apply a quantifier to the entire group or to restrict alternation to part of the regex. Only parentheses can be used for grouping.


1 Answers

\[ doesn't do what you think it does. Nor is Regex the same as in Perl. Try this instead:

Regex.Replace(sql, @"[\[\]']+", "");
like image 199
Blindy Avatar answered Oct 16 '22 23:10

Blindy