Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8110557
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T01:47:51+00:00 2026-06-06T01:47:51+00:00

I have a set of characters I want to remove from a string :

  • 0

I have a set of characters I want to remove from a string : "/\[]:|<>+=;,?*'@

I’m trying with :

private const string CHARS_TO_REPLACE = @"""/\[]:|<>+=;,?*'@";

private string Clean(string stringToClean)
{
    return Regex.Replace(stringToClean, "[" + Regex.Escape(CHARS_TO_REPLACE) + "]", "");
}

However, the result is strictly identical to the input with something like "Foo, bar and other".

What is wrong in my code ?

This looks like a lot to this question, but with a black list instead of a white list of chars, so I removed the not in ^ char.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-06T01:47:52+00:00Added an answer on June 6, 2026 at 1:47 am

    The problem is a misunderstanding of how Regex.Escape works. From MSDN:

    Escapes a minimal set of characters (\, *, +, ?, |, {, [, (,), ^, $,., #, and white space) by replacing them with their escape codes.

    It works as expected, but you need to think of Regex.Escape as escaping metacharacters outside of a character class. When you use a character class, the things you want to escape inside are different. For example, inside a character class - should be escaped to be literal, otherwise it could act as a range of characters (e.g., [A-Z]).

    In your case, as others have mentioned, the ] was not escaped. For any character that holds a special meaning within the character class, you will need to handle them separately after calling Regex.Escape. This should do what you need:

    string CHARS_TO_REPLACE = @"""/\[]:|<>+=;,?*'@";
    string pattern = "[" + Regex.Escape(CHARS_TO_REPLACE).Replace("]", @"\]") + "]";
    
    string input = "hi\" there\\ [i love regex];@";
    string result = Regex.Replace(input, pattern, "");
    Console.WriteLine(result);
    

    Otherwise, you were ending up with ["/\\\[]:\|<>\+=;,\?\*'@], which doesn’t have ] escaped, so it was really ["/\\\[] as a character class, then :\|<>\+=;,\?\*'@] as the rest of the pattern, which wouldn’t match unless your string matched exactly those remaining characters.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this string that has illegal chars that I want to remove but
I am trying to validate a string based on the inputed characters. I want
How would I create a unique set of characters from a given string? For
I have a random string set in each array index.And i want to give
Let say I have set of characters [a-z] . I want to match every
I have a set of strings containing characters in a PHP script, I need
I have an NSString which is splitted into set of characters. I need to
I have the following animation set up using Three.js: http://frontier.lincoln.ac.uk/3d/development/Stage2/characters/man2.html Please ignore the ropey
i have this set of characters: peach_addict 612 EmailName 747 littefooted@aol 805-582-73 748 cell
I need to extract a set of characters in of a string. I plan

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.