Remove square brackets and single quotes regex not working

11,219

Solution 1

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

Regex.Replace(sql, @"[\[\]']+", "");

Solution 2

Is this what you're looking for? ['\\/]

That should match any single character that is a slash or single quote.

Solution 3

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.

Solution 4

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.

Share:
11,219
Saif Khan
Author by

Saif Khan

Updated on June 06, 2022

Comments

  • Saif Khan
    Saif Khan about 2 years

    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