I'm facing an issue with removing escape sequence from string using C#
var street = "1324 W. 650 N.\t";
Above I mentioned my code please check once and mention below comments on how to remove escape sequences like "\t".
If you only want to remove '\t' try:
var street = "1324 W. 650 N.\t";
var removed = street.Replace("\t", "");
In case you would also like to remove '\n' and '\r' too, you can concatenate the operation 'Replace()'.
var street = "1324 W. 650 N.\t";
var removed = street.Replace("\t", "").Replace("\n", "").Replace("\r", "");
Check the example I made.
You'll want to use Regex.Unescape method.
String unescapedString = Regex.Unescape(textString);
However, becareful that the Regex.Unescape doesn't un-escape ". According to the documentation, it does the following:
..by removing the escape character ("") from each character escaped by the method. These include the , *, +, ?, |, {, [, (,), ^, $,., #, and white space characters. In addition, the Unescape method unescapes the closing bracket (]) and closing brace (}) characters.
Related
I have requirement to remove all the special characters from any string except " and ' .
ClientName = Regex.Replace(ClientName, #"\(.*?\)", " ").Trim();
This is the regex I am using. I want exclude all the special characters except " and '.
Example:
clientName= "S"unny, Cool. Mr"
Output should be
"S"unny Cool Mr"
Consider using the following pattern:
#"[^\p{L}\p{Nd}'""\s]+"
This will target all special characters while also excluding single and double quote, as well as whitespace.
string clientName = "S\"unny, Cool. Mr";
string output = Regex.Replace(clientName, #"[^\p{L}\p{Nd}'""]+", "");
Console.WriteLine(output);
This prints:
S"unny Cool Mr
The character classes \p{L} and \p{N} represent all Unicode letters and numbers, so placing them into a negative character class means remove anything which is not a number or letter.
Regex.IsMatch method returns the wrong result while checking the following condition,
string text = "$0.00";
Regex compareValue = new Regex(text);
bool result = compareValue.IsMatch(text);
The above code returns as "False". Please let me know if i missed anything.
The Regex class has a special method for escaping characters in a pattern: Regex.Escape()
Change your code like this:
string text = "$0.00";
Regex compareValue = new Regex(Regex.Escape(text)); // Escape characters in text
bool result = compareValue.IsMatch(text);
"$" is a special character in C# regex. Escape it first.
Regex compareValue = new Regex(#"\$0\.00");
bool result = compareValue.IsMatch("$0.00");
Regex expressions: https://msdn.microsoft.com/en-us/library/az24scfc(v=vs.110).aspx
Both '.' and '$' are special characters and thus you need to escape them if you want to match the character itself. '.' matches any character and '$' matches the end of a string
see: https://regex101.com/r/pK2uY6/1
You have to escape $ since it is a special (reserved) character which means "end of string". In case . means just dot (say, decimal separator) you have to escape it as well (when not escaped, . means "any symbol"):
string pattern = #"\$0\.00";
bool result = RegEx.IsMatch(text, pattern);
As for your original pattern, it has no chance to match any string, since $0.00 means
$ end of string, followed by
0 zero
. any character
0 zero
0 zero
but end of string can't be followed by...
I have a list of special characters that includes ^ $ ( ) % . [ ] * + - ?. I want put % in front of this special characters in a string value.
I need this to generate a Lua script to use in Redis.
For example Test$String? must be change to Test%$String%?.
Is there any way to do this with regular expressions in C#?
In C#, you just need a Regex.Replace:
var LuaEscapedString = Regex.Replace(input, #"[][$^()%.*+?-]", "%$&");
See the regex demo
The [][$^()%.*+?-] character class will match a single character, either a ], [, $, ^, (, ), %, ., *, +, ? or - and will reinsert it back with the $& backreference in the replacement pattern pre-pending with a % character.
A lookahead is just a redundant overhead here (or a show-off trick for your boss).
You can use lookaheads and replace with %
/(?=[]*+$?)[(.-])/
Regex Demo
(?=[]*+$?)[(.-]) Postive lookahead, checks if the character following any one from the altenation []. If yes, substitutes with %
You can use this regex: ([\\^$()%.\\[\\]*+\\-?])
It will match and capture characters inside the character class. Then you can use $1 to reference the captured character and insert % before it, like so: %$1.
Here is an example code and demo:
string input = "Test$String?";
string pattern = "([\\^$()%.\\[\\]*+\\-?])";
string replacement = "%$1";
Regex rgx = new Regex(pattern);
string result = rgx.Replace(input, replacement);
Console.WriteLine("Original String: {0}", input);
Console.WriteLine("Replacement String: {0}", result);
You can use (?=[\^\$()\%.[]*+-\?]) regex replaced String as "%"
i am currenty having a problem related with regex.replace . I have an item in checkedlistbox that contains a string with parenthesis "()" :
regx2[4] = new Regex( "->" + checkedListBox1.SelectedItem.ToString());
the example setence inside the selected item is
hello how are you (today)
i use it in regex like this :
if (e.NewValue == CheckState.Checked)
{
//replaces the string without parenthesis with the one with parenthesis
//ex:<reason1> ----> hello, how are you (today) (worked fine)
richTextBox1.Text = regx2[selected].Replace(richTextBox1.Text,"->"+checkedListBox1.Items[selected].ToString());
}
else if (e.NewValue == CheckState.Unchecked)
{
//replaces the string with parenthesis with the one without parenthesis
//hello, how are you (today)----><reason1> (problem)
richTextBox1.Text = regx2[4].Replace(richTextBox1.Text, "<reason" + (selected + 1).ToString() + ">");
}
it is able to replace the string on the first condition but unable to re-replace the setences again on second because it has parenthesis "()", do you know how to solve this problem??
thx for the response :)
Instead of:
regx2[4] = new Regex( "->" + checkedListBox1.SelectedItem.ToString());
Try:
regx2[4] = new Regex(Regex.Escape("->" + checkedListBox1.SelectedItem));
To use any of the special characters as a literal in a regex, you need to escape them with a backslash. If you want to match 1+1=2, the correct regex is 1\+1=2. Otherwise, the plus sign has a special meaning.
http://www.regular-expressions.info/characters.html
special characters:
backslash \,
caret ^,
dollar sign $,
period or dot .,
vertical bar or pipe symbol |,
question mark ?,
asterisk or star *,
plus sign +,
opening parenthesis (,
closing parenthesis ),
opening square bracket [,
opening curly brace {
To fix it you could probably do this:
regx2[4] = new Regex("->" + checkedListBox1.SelectedItem.ToString().Replace("(", #"\(").Replace(")", #"\)"));
But I would just use string.replace() since you aren't doing any parsing. I can't tell what you're transforming from/to and why you use selected as an index on the regex array in the if and 4 as the index in the else.
I am using the below code to remove some unwanted characters from the email. but the Regex replaces the '.' symbol, even though I did not mentioned in the character set to be removed.
string emailText = #"[\s;'\(\)\[\]!#\$%&\*\+-\?>=<_:\/\""]";
var stringInput = Console.ReadLine(); //Input "sara#gmail.com"
var stringTest = Regex.Replace(stringInput, emailText,string.Empty); //Output "sara#gmailcom"
Kindly help me to resolve this issue.
Thanks & Regards,
Saravanakumar R.
In the regexp, \+-\? means the range of ASCII characters between + and ?, which includes lots of punctuation characters, including ..
You should escape the - to prevent it from meaning a character range. On the other hand, you don't need to escape most of the other special characters inside [].
Move - to the beginning(or end) of the [...].
+-\? cause all character between +(ASCII 43) and ?(ASCII 63) to match.
.(ASCII 45) is included in that range.
Minus(-) should also be escaped.
string emailText = #"[\s;'\(\)\[\]!#\$%&\*\+\-\?>=<_:\/\""]";