I need to replace a string with specific list of special characters to string.Empty.
Example: 123 ~Main To 123 Main
List of Special character : + - && || ! ( ) { } [ ] ^ " ~ * ? : \
I know that we can do like this below, can we have better way, using some Regular expressions.
> keyword = keyword.Replace("+", string.Empty);
> keyword = keyword.Replace("&&", string.Empty);
> keyword = keyword.Replace("||", string.Empty);
> keyword = keyword.Replace("!", string.Empty);
> keyword = keyword.Replace("(", string.Empty);
> keyword = keyword.Replace(")", string.Empty);
> keyword = keyword.Replace("{", string.Empty);
> keyword = keyword.Replace("}", string.Empty);
> keyword = keyword.Replace("[", string.Empty);
> keyword = keyword.Replace("]", string.Empty);
> keyword = keyword.Replace("^", string.Empty);
> keyword = keyword.Replace("~", string.Empty);
> keyword = keyword.Replace("*", string.Empty);
> keyword = keyword.Replace("?", string.Empty);
> keyword = keyword.Replace(":", string.Empty);
> keyword = keyword.Replace("\\", string.Empty);
> keyword = keyword.Replace("\"", string.Empty);
Thanks in advance.
You can build regex on the fly - \+|&&|\|\||!|\(|\)|\{|}|\[|]|\^|~|\*|\?|:|\\|":
string input = "Hello world!";
string[] replaceables = new[] { "+", "&&", "||", "!", "(", ")", "{", "}", "[", "]", "^", "~", "*", "?", ":", "\\", "\"" };
string rxString = string.Join("|", replaceables.Select(s => Regex.Escape(s)));
string output = Regex.Replace(input, rxString, string.Empty);
You also can optimize regex as @Robin suggested (though the generation becomes complicated) - [\+!\(\)\{}\[\]\^~\*\?:\\"]|&&|\|\|
string rxString = string.Join("|", replaceables.GroupBy(r => r.Length > 1)
.Select(g => g.Key ? string.Join("|", g.Select(r => Regex.Escape(r)))
: string.Format("[{0}]", string.Join(string.Empty, g.Select(r => r == "]" ? "\\]" : Regex.Escape(r))))));
Slightly more clean regex (only metacharacters between square brackets are escaped) - [+!(){}[\]\^~*?:\\"]|&&|\|\|.
string rxString = string.Join("|", replaceables.GroupBy(r => r.Length > 1)
.Select(g => g.Key ? string.Join("|", g.Select(r => Regex.Escape(r)))
: string.Format("[{0}]", string.Join(string.Empty, g.Select(r => new[] { "]", @"\", "-", "^" }.Contains(r) ? @"\" + r : r)))));
The cleanest possible regex (additional validation for requirements to escape metacharacters) - [+!(){}[\]^~*?:\\"]|&&|\|\|
string rxString = string.Join("|", replaceables.GroupBy(r => r.Length > 1)
.Select(g => g.Key ? string.Join("|", g.Select(r => Regex.Escape(r)))
: string.Format("[{0}]", string.Join(string.Empty, g.Select((r, i) => r == @"\" || r == "]" && g.Count() > 1 || r == "^" && i == 0 || r == "-" && i > 0 ? @"\" + r : r)))));
If you have strings like Hello &|&&|& complicated world!, then you need to pass it several times.
string output = input;
string outputRef = output;
do
{
outputRef = output;
output = Regex.Replace(output, rxString, string.Empty);
} while (output != outputRef);
Console.WriteLine(output); // Hello complicated world
try this...
//to remove non alphanumeric characters (special characters) from a string?
public static string removespclchr(string input)
{
Regex regex = new Regex("[^a-zA-Z0-9]");
return regex.Replace(input, "");
}
or
string q = Regex.Replace(query, @""|['"",&?%\.*:#/\\-]", " ").Trim();
or
string replacestr= Regex.Replace(str, "[^a-zA-Z0-9_]+", " ");
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With