This question already has answers here:
Regular expression to allow backslash in C#
(3 answers)
Closed 3 years ago.
I created my own regex, and everything work fine except the backslash thing. I tried my versions, but none of them helped.
var regexItem = new Regex("[^A-Za-z0-9_.,&/*:;=+{}()'\"\\ -]+");
string temp2 = "";
while ((#line = file2.ReadLine()) != null)
{
if (regexItem.IsMatch(line) && (line.Contains(".jpg") || line.Contains(".png") || line.Contains(".jpeg") || line.Contains(".svg")))
{
#temp2 = Regex.Replace(line, "[^A-Za-z0-9_.,&/\\*:;=+{}()'\" -]+", "");
postki.WriteLine(#temp2);
Console.WriteLine(#"{0} ==> {1}", #line, #temp2);
}
else
{
postki.WriteLine(#line);
}
}
In c# string literals, special characters (like ") need to be escaped. C# uses the \ to escape those characters.
For example - as you already did - a string containing a " would be declared like that:
string quote = "\"";
So of course the backslash itself needs escapting, too. So a string containing a back-slash would look like this:
string backslash = "\\";
So with two backslashs in the literal, you have one real backslash in the string.
But a backslash also is a special character in regular expressions (also used to escape other symbols, for example if you mean a literal . instead of the any-character-dot, you use \.). So to use a literal backslash (meaning to match a single backslash in the search string) you need to use 4 back-slashes in your c# string literal:
string regexBackSlash = "\\\\";
In your regex you probably meant:
var regexItem = new Regex("[^A-Za-z0-9_.,&/*:;=+{}()'\"\\\\ -]+");
// difference here ^
Or you may use verbatim string literals (with a leading #):
var regexItem = new Regex(#"[^A-Za-z0-9_.,&/*:;=+{}()'""\\ -]+");
(but then again you need to escape the " with a second one).
Related
I have a string like "aaa\\\\\\\\test.txt".
How do I replace all the repeating \\ characters by a single \\?
I have tried
pPath = new Regex("\\{2,}").Replace(pPath, Path.DirectorySeparatorChar.ToString());
which matches on http://regexstorm.net/tester but doesn't seem to do the trick in my program.
I'm running this on Windows so the Path.DirectorySeparatorChar is a \\.
Use new Regex(#"\\{2,}") and the rest the same.
You need to actually leave the backslash escaped in your regular expression, so you need to produce a string with two backslashes in it. The two equivalent techniques to produce the correct C# string literal are #"\\{2,}" or "\\\\{2,}"
Both of those string literals are the string \\{2,}, which is the correct regular expression. Your regular expression calls for one backslash character occurring two times, and you have to escape the backslash character. At the risk of being pedantic, if you wanted to replace two a characters, you would use the regular expression a{2,} and if you want to replace to \ characters, you would use the regular expression \\{2,} because \\ is the regular expression that matches a single \. Clear as mud? :)
Not being a demi-god at regex, I would use StringBuilder and do something like this:
string txt = "";
int count = 0;
StringBuilder bldr = new StringBuilder();
foreach(char c in txt)
{
if (c == '\')
{
count++;
if (count < 3)
{
bldr.Append(c);
}
}
else
{
count = 0;
bldr.Append(c);
}
}
string result = bldr.ToString();
string testStr="thestringhasa\slash";
if(testStr.Contains("\"))
{
//Code to process string with \
}
How do I properly test to see if a string contains a backslash, when I try the it statement if says a New Line in constant.
The other two answers are entirely correct, but no one bothered to explain why. The \ character has a special purpose in C# strings. It is the escape character, so to have a string that contains a slash, you have to use one of two methods.
Use the string literal symbol #. A string preceded by the # symbol tells the C# compiler to treat the string as a literal and not escape anything.
Use the escape character to tell the C# compiler there is a special character that is actually part of the string.
So, the following strings are equivalent:
var temp1 = #"test\test";
var test2 = "test\\test";
test1 == test2; // Yields true
You should use double slashes
string testStr=#"thestringhasa\slash";
if(testStr.Contains("\\"))
{
//Code to process string with \
}
The backslash must be escaped. Try the following:
string testStr = #"thestringhasa\slash";
if (testStr.Contains("\\"))
{
//Code to process string with \
}
This question already has answers here:
Escape Special Character in Regex
(3 answers)
Closed 7 years ago.
I'm trying to extract MTQ0ODQ3NjcyNDoxNDQ4NDc2NzI0OjE6LTM4OTc1OTc2MjM4MDc1OTM2NjY6MTQ0ODQ3NjAwMzowOjA6NTQw from the string below.
I am having issues with the \\ (backslash) characters. How do I escape these in C#. Is there any documentation that shows characters that need escaping in regex patterns, and how to escape them?
first_cursor\\":\\"MTQ0ODQ3NjcyNDoxNDQ4NDc2NzI0OjE6LTM4OTc1OTc2MjM4MDc1OTM2NjY6MTQ0ODQ3NjAwMzowOjA6NTQw\\"
I've tried the following to no avail. I tried to avoid having to escape the backslashes altogether:
MatchCollection matches = Regex.Matches(content, "first_cursor*.quot;([-0-9A-Za-z]+)");
Any help would be much appreciated.
In C# each backslash in a string can be written as \\\\.
You can use the following:
MatchCollection matches = Regex.Matches(content, "first_cursor\\\\{2}":\\\\{2}"([-0-9A-Za-z]+)");
I prefer to use verbatim string literals when writing RegEx strings in C#:
string pattern = #"first_cursor\\\\":\\\\"([-0-9A-Za-z]+)\\\\"";
This prevents you from having to escape the slashes twice; once for C# and again for the RegEx engine.
As an aside, this syntax is also useful when storing paths in strings:
string logFile = #"C:\Temp\mylog.txt";
And even supports multi-line for SQL commands and such:
string query = #"
SELECT *
FROM tblStudents
WHERE FirstName = 'Bobby'
AND LastName = 'Tables'
";
You can use lookahead to elimate some of the contenders:
var example = #"first_cursor\\":\\"MTQ0ODQ3NjcyNDoxNDQ4NDc2NzI0OjE6LTM4OTc1OTc2MjM4MDc1OTM2NjY6MTQ0ODQ3NjAwMzowOjA6NTQw\\"";
var regex = new Regex("(?<!&[-0-9A-Za-z]*)(?<!_[-0-9A-Za-z]*)[-0-9A-Za-z]+");
var matches = regex.Matches(example);
foreach(var match in matches)
{
if (match.ToString() != "first")
{
Console.WriteLine(match);
}
}
This would give you two matches. One for first and one for the string you are looking for. Then you can iterate over the matches and see if it's not "first" then it should be what you are looking for.
Using C# we can do string check like if string.contains() method, e.g.:
string test = "Microsoft";
if (test.Contains("i"))
test = test.Replace("i","a");
This is fine. But what if I want to replace a string which contains " symbol to be replaced.
I want to achieve this:
"<html><head>
I want to remove the " symbol present in check so that the result would be:
<html><head>
The " character can also be replaced, just like any other:
test = test.Replace("\"","");
Also, note that you don't have to test if the character exists : your test.Contains("i") could be removed since the .Replace() method won't do anything (no replace, no error thrown) if the character doesn't exist inside the string.
To include a quote symbol in a string, you need to escape it, using a backslash. In your example, you want to use something lik this:
if (test.Contains("\""))
There are two ways to include a '"' character in a string literal. All the answers so far have used the c-style way:
var quotation = "Parting is such sweet sorrow";
var howSweetIsIt = quotation + " that I shall say \"good-night\" till it be morrow.";
In some contexts (especially for users experienced with Visual Basic), the verbatim string literal may be easier to read. A verbatim string literal begins with an # sign, and the only character that requires escaping is the quotation mark -- all other characters are included verbatim (hence the name). Significantly, the method of escaping the quotation mark is different: rather than preceding it with a backslash, it must be doubled:
var howSweetIsIt = quotation + " that I shall say ""good-night"" till it be morrow.";
string SymbolString = "Micro\"so\"ft";
The string above use scape char \ to insert " between the characters
string Result = SymbolString.Replace("\"", string.Empty);
With the following replace I replace the character "" for empty.
This is what you try to achieve?
if (check.Contains("\"")
output = check.Replace("\"", "");
output = check.Replace("\"", "");
Just remember to use "\"" for the quote sign as the backslash is an escape character.
if (str.Contains("\""))
{
str = str.Replace("\"", "");
}
This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
What does # mean at the start of a string in C#?
Sorry but I can't find this on Google. I guess it maybe is not accepting my search string when I do a search.
Can someone tell me what this means in C#
var a = #"abc";
what's the meaning of the #?
It is a string literal. Which basically means it will take any character except ", including new lines. To write out a ", use "".
The advantage of #-quoting is that escape sequences are not processed,
which makes it easy to write, for example, a fully qualified file
name:
#"c:\Docs\Source\a.txt" // rather than "c:\\Docs\\Source\\a.txt"
It means it's a literal string.
Without it, any string containing a \ will consider the next character a special character, such as \n for new line. With a # in front, it will treat the \ literally.
In the example you've given, there is no difference in the output.
This says that the characters inside the double quotation marks should be interpreted exactly as they are.
You can see that the backslash is treated as a character and not an
escape sequence when the # is used. The C# compiler also allows you to
use real newlines in verbatim literals. You must encode quotation
marks with double quotes.
string fileLocation = "C:\\CSharpProjects";
string fileLocation = #"C:\CSharpProjects";
Look at here for examples.
C# supports two forms of string literals: regular string literals and verbatim string literals.
A regular string literal consists of zero or more characters enclosed
in double quotes, as in "hello", and may include both simple escape
sequences (such as \t for the tab character) and hexadecimal and
Unicode escape sequences.
A verbatim string literal consists of an # character followed by a
double-quote character, zero or more characters, and a closing
double-quote character. A simple example is "hello". In a verbatim
string literal, the characters between the delimiters are interpreted
verbatim, the only exception being a quote-escape-sequence. In
particular, simple escape sequences and hexadecimal and Unicode
escape sequences are not processed in verbatim string literals. A
verbatim string literal may span multiple lines.
Code Example
string a = "hello, world"; // hello, world
string b = #"hello, world"; // hello, world
string c = "hello \t world"; // hello world
string d = #"hello \t world"; // hello \t world
string e = "Joe said \"Hello\" to me"; // Joe said "Hello" to me
string f = #"Joe said ""Hello"" to me"; // Joe said "Hello" to me
string g = "\\\\server\\share\\file.txt"; // \\server\share\file.txt
string h = #"\\server\share\file.txt"; // \\server\share\file.txt
string i = "one\r\ntwo\r\nthree";
string j = #"one
two
three";
Reference link: MSDN