Replace characters where certain character does not follow a comma? - c#

Is there a way to use wildcards to define the following:
I would like a "\" to come before and after a comma, when a comma character does not contain a "\"" before it or after it.
I am a little unsure how to do the negation.
EDIT Sample data:
"col1,col2,col3"
should become
"\"col1\",\"col2\",\"col3\""
where "\"" just means a quote string

Use the "negative look behind" assertion:
(?<!\\),
Can't give you a better answer without having sample input/output.

Try (?<!\"),(?!\"), which is called Zero-Width Assertions
I'll busy now, would explain later, sorry for that.

Replace everything that matches the following: ^(\\\"),^(\\\") with: \",\"
It means anything but a backslash followed by a quote, followed by a comma, followed by anything but a backslash followed by a quote.

Use regular expressions or a simple replace:
string s = "col1,col2\",\"col3";
// replace all existing quotes and replace all commas with escaped characters again
string r = s.Replace('\"','').Replace(",","\",\"");
// r = "col1\",\"col2\",\"col3"
But this does not do what your sample data looks like:
"col1,col2,col3" should become "col1\",\"col2\",\"col3\""
This isn't following your rule (look at the trailing \" !). Maybe you want to wrap all col's, so you can add a \" at the beginning and the end, too. (Assuming the seperator is always just ,, not including spaces)

I know this thread is a bit old but for the new visitors this can also be done:
string sample = "col1,col2,col3"
string result = sample.Replace("""","");
result = "\"" + result.replace(",","\",\"") + "\""
Hope it helps!

Related

Removing Sub-string with some pattern from a string

I have a string something like JSON format:
XYZ DIV Parameters: width=\"1280\" height=\"720\", session=\"1\"
Now I want to remove width=\"1280\" height=\"720\" from this string.
Note: There can be any number in place of 1280 and 720. So, I can't just replace it with null.
Please tell me how to solve it? Either by Regex or any other better method possible.
Regex to be replaced with empty string:
(width|height)=\\"\d+\\"
Regex visualization:
Code:
string input = #"XYZ DIV Parameters: width=\""1280\"" height=\""720\"", session=\""1\""";
string output = Regex.Replace(input, #"(width|height)=\\""\d+\\""", string.Empty);
You could do a find and replace using the following regex:
width=\\"\d*+\\" replace with a blank string, as well as replacing height=\\"\d*+\\" with a blank string.
This is removing the entire text of width=\"XYZ\", if you wanted to just replace the numbers or blank out the numbers you can replace with a string that suits your needs (width=\"\" for example)
If you can guarantee the width and height will ALWAYS be in that format and ALWAYS follow each other seperated by a space, you can combine that into one bigger regex find/replace using width=\\"\d*+\\" height=\\"\d*+\\".
A little more explanation on the regex so you take something away, not just a quick fix :)
width=\\"\d*+\\" breaks down to:
width= pretty simple, just find the text you are looking for to start your removal.
\\" since \ is a special char in regex you have to escape it, then the " char can just follow it up like normal.
\d*+ digits \d, zero or more of them *, and then non greedy +. The important part here is the non greedy on the digits. If you left that off, your regex would look and consume digits until it found the last ". Not 100% needed in your case (since height is buffering) but it is still a lot safer.
\\" to end the regex out
This will do it:
string resultString = null;
try {
Regex regexObj = new Regex(#"^(.*?)width=\\"".*?\\"" height=\\"".*?\\""(.*?)$", RegexOptions.IgnoreCase);
resultString = regexObj.Replace(subjectString, #"$1width=\""\"" height=\""\""$2");
} catch (ArgumentException ex) {
// Syntax error in the regular expression
}

How can I add \ symbol to the end of string in C#

Please forgive me a beginner's question :)
string S="abc";
S+="\";
won't complile.
string S="abc";
S+="\\";
will make S="abc\\"
How can I make S="abc\" ?
Your second piece of code is what you want (or a verbatim string literal #"\" as others have suggested), and it only adds a single backslash - print it to the console and you'll see that.
These two pieces of code:
S += "\\";
and
S += #"\";
are exactly equivalent. In both cases, a single backslash is appended1.
I suspect you're getting confused by the debugger view, which escapes backslashes (and some other characters). You can validate that even with the debugger by looking at S.Length, which you'll see is 4 rather than 5.
1 Note that it doesn't change the data in the existing string, but it sets the value of S to refer to a new string which consists of the original with a backslash on the end. String objects in .NET are immutable - but that's a whole other topic...
Try this:
String S = "abc";
S += #"\";
# = verbatim string literal
http://msdn.microsoft.com/en-us/library/aa691090%28v=vs.71%29.aspx
http://msdn.microsoft.com/en-us/library/vstudio/362314fe.aspx
string S = "abs" + "\\";
Should and does result in abc\.
What you are probably seeing is the way the debugger/intellisense visualizes the string for you.
Try printing your string to the console or display it in a textbox.
You already have the solution. The reason it appears as abc\\ whilst debugging is because VS will escape backslashes, print the value of S to a console window and you'll see abc\.
You could add an # to the start of the string literal, e.g.
string S="abc";
S+= #"\";
Which will achieve the same thing.
You can escape the backslash with the # character:
string S="abc";
S += #"\";
But this accomplishes exactly what you've written in your second example. The confusion on this is stemming from the fact that the Visual Studio debugger continues to escape these characters, even though your source string will contain only a single backslash.
Your second example is perfectly fine
string S="abc";
S+="\\";
Visual studio displays string escaped, that's why you see two slashes in result string. If you don't want to use escaping declare string like this
#"\"
This is not compiling because compiler is expecting a character after escape symbol
string S="abc";
S+="\";
string S="abc";
S+="\\";
Console.WriteLine(S); // This is what you're missing ;)
You'll see your string is not wrong at all.
The backslash (\) is an escape character, and allows you to get special characters that you wouldn't normally be able to insert in a string, such as "\r\n", which represents a NewLine character, or "\"" which basically gives you a " character.
In order to get the \ character, you need to input "\\" which is exactly what you're doing and also what you want.
Using the verbatim (#) replaces all occurrences of \ into \\, so #"\" == "\\". This is usually used for paths and regexes, where literal \ are needed in great numbers. Saying #"C:\MyDirectory\MyFile" is more comfortable than "C:\\MyDirectory\\MyFile" after all.
Try this
string s="abc";
s = s+"\\";

replace unicode character

String jData="Memur adayar\u0131n\u0131n en b\u00fcy\u00fck sorunar"
+ "\u0131ndan KPSS \u0 131 ";
jData = Regex.Replace(jData, #"\\u0 ", #"\\u0", RegexOptions.Compiled).Trim();
I have to replace "\u0 " in jData with "\u0" (i.e. remove the trailing whitespace character if there is one) but the method I used isn't working. What should I do?
So you've got some malformed Unicode escapes in the string and you want to fix them by removing any whitespace after the 0. That's simple enough:
jData = Regex.Replace(jData, #"(\\u0)\s+(\w+)", "$1$2");
The hardest part of all this is figuring out what all the backslashes are supposed to mean. C# can helps you with that supports an alternative string literal syntax for verbatim string, the only character that you have to escape with a backslash is the backslash itself. (You have to escape quotation marks too, but you do that with another quote, i.e. "").
With that out of the way, the real reason I answered this question was to advise you not to use RegexOptions.Compiled. I'm sure you've heard many people say it makes the regex work faster. That's true, but it's an oversimplification. Read this article for a good discussion of this issue. Do yourself a favor and forget RegexOptions.Compiled even exists until you run into a problem you can't solve without it.
find: #"\\u0 "
replace: #"\\u0"
they are the same. Try it with an capital O or normal o
I think I got it working
string jData= #"Memur adayar\u0131n\u0131n en b\u00fcy\u00fck sorunar\u0131ndan KPSS \u0 131 ";
jData = Regex.Replace(jData, #"\\u0 ", #"\u0", RegexOptions.Compiled).Trim();
Notice I added an extra '#' in front of the input string. And in the regex part I changed the third argument to #"\u0"
There's a problem with your example string. I'm supposing that you actually wanted the backslashes in the string, in which case the simplest approach is to put # before the string literals. And then I believe you have the opposite problem in the second line, where you should have either used just one backslash in each string, or omitted the #.
There's no reason to use Regex.Replace() here. jData.Replace() would suffice just fine:
String jData=#"Memur adayar\u0131n\u0131n en b\u00fcy\u00fck sorunar"
+ #"\u0131ndan KPSS \u0 131 ";
jData = jData.Replace(#"\u0 ", #"\u0").Trim();

regex.replace #number;#

What would be the regex expression to find (PoundSomenumberSemiColonPound) (aka #Number;#)? I used this but not working
string st = Regex.Replace(string1, #"(#([\d]);#)", string.Empty);
You're looking for #\d+;#.
\d matches a single numeric character
+ matches one or more of the preceding character.
(\x23\d+\x3B\x32)
# and / are both used around patterns, thus the trouble. Try using the above (usually when I come in to trouble with specific characters I revert to their hex facsimile (asciitable.com has a good reference)
EDIT Forgot to group for replacement.
EDITv2 The below worked for me:
String string1 = "sdlfkjsld#132;#sdfsdfsdf#1;#sdfsdfsf#34d;#sdfs";
String string2 = System.Text.RegularExpressions.Regex.Replace(string1, #"(\x23\d+\x3B\x23)", String.Empty);
Console.WriteLine("from: {0}\r\n to: {1}", string1, string2);;
Output:
from: sdlfkjsld#132;#sdfsdfsdf#1;#sdfsdfsf#34d;#sdfs
to: sdlfkjsldsdfsdfsdfsdfsdfsf#34d;#sdfs
Press any key to continue . . .
You don't need a character class when using \d, and as SLaks points out you need + to match one or more digits. Also, since you're not capturing anything the parentheses are redundant too, so something like this should do it
string st = Regex.Replace(string1, #"#\d+;#", string.Empty);
You may need to escape the # symbols, they're usually interpreted as comment markers, in addition to #SLaks comment about using + to allow multiple digits

Regex.Replace() for replacing the whole occurrence

Am using regex.Replace() to replace the whole occurrence of a string.. so I gave like Regex.Replace(str,#stringToReplace,"**"); where stringToReplace = #"session" + "\b";
if i give like that its not replacing.. but if i give like Regex.Replace(str,#"session\b","**"); then its working.. how to avoid this.. i want to pass value which will be set dynamically..
Thanks
nimmi
try
stringToReplace = #"session" + #"\b";
The # here means a verbatim string literal.
When you write "\b" without the # it means the backspace character, i.e. the character with ASCII code 8. You want the string consisting of a backslash followed by a b, which means a word boundary when in a regular expression.
To get this you need to either escape the backslash to make it a literal backslash: "\\b" or make the second string also into a verbatim string literal: #"\b". Note also that the # in #"session" (without the \b) doesn't actually have an effect, although there is no harm in leaving it there.
stringToReplace = "session" + #"\b";
#"session" + "\b"
and
#"session\b"
are not the same string.
In the first case "\b" you don't treat the slash as a slash but as an escape parameter. In the second case you do.
So #"session" + #"\b" should bring the same result

Categories

Resources