This question already has answers here:
Are there any CSV readers/writer libraries in C#? [closed]
(5 answers)
Closed 6 years ago.
I've been trying my luck with Regex but my understanding doesn't seem to be the best.
Problem
I have a .csv file given to me by a 3rd party. I cannot edit it but need to read the data into my application.
There are always 12 columns in the file. However, sometimes it will go like this:
text, text ,text,"text with comma,"
text, text, text, text....
text, text, text,"text with comma,","text with comma again", text...
What I need to do this replace all the commas between the "" with a -.
Any help would be appreciated!
This might do the trick for you
foreach(Match match in Regex.Matches(YourCSV, "\"([^\"]*)\""))
if(match.ToString().Contains(","))
YourCSV = YourCSV.Replace(match.ToString(), match.ToString().Replace(",", "-"));
Related
This question already has answers here:
Efficient way to remove ALL whitespace from String?
(18 answers)
Closed 3 years ago.
I'm looking for a efficient way for removing all of the white spaces in an string.
I have checked replace (replace(' ','')) but I'm looking for a more efficient way.
I'd appreciate the help.
You may use Regular Expression.
For example:
var result=System.Text.RegularExpressions.Regex.Replace(input, #"\s+", "");
Input is your string
See more Removing whitespaces using C#
This question already has answers here:
How can you strip non-ASCII characters from a string? (in C#)
(15 answers)
C# regex to remove non - printable characters, and control characters, in a text that has a mix of many different languages, unicode letters
(4 answers)
Closed 4 years ago.
I'm reading data from a file, and sometimes the file contains funky stuff, like:
"䉌Āᜊ»ç‰ç•‡ï¼ƒè¸²æœ€ä²’Bíœë¨¿ä„€å•²ï²ä‹¾é¥˜BéŒé“‡ä„€â²ä‹¾â¢"
I need to strip/replace these characters as JSON has no idea what to do with them.
They aren't control characters (I think), so my current regex of
Regex.Replace(value, #"\p{C}+", string.Empty);
Isn't catching them.
A lot of these strings read in are going to be long, upwards of256 characters, so I'd rather not loop through each char checking it.
Is there a simple solution to this? I'm thinking regular expressions would solve it, but I'm not sure.
If all you want is ASCII then you could do:
Regex.Replace(value, #"[^\x00-\x7F]+", string.Empty);
and if all you want are the "normal" ASCII characters, you could do:
Regex.Replace(value, #"[^\x20-\x7E]+", string.Empty);
This question already has answers here:
Split string using backslash
(3 answers)
Closed 4 years ago.
I'm looking for the best way to extract the computer name from a predictably formatted string. The string will always be in this format:
C:\\Folder1\\Folder2\\NOOBCOMPUTER\\...
If there is a way to extract the contents of a string between the third pair of backslashes and the fourth pair, that should work.
Though I have no idea where to begin with the regex to achieve that, nor do I know if regex is the most "foolproof" way of going about this in C#.
You can split the string and then inspect each element.
string [] s = yourstring.Split("\\");
string final = s[3];
This question already has answers here:
Parse subtitle file using regex C#
(5 answers)
Closed 8 years ago.
I need to split an .srt file text like
1
00:02:10,437 --> 00:02:11,598
Day one, Greenie.
2
00:02:11,757 --> 00:02:12,838
Rise and shine.
3
00:02:14,357 --> 00:02:16,041
He looks like
a slopper to me.
split into multi-line string array, each string has at least 3 lines,
one for the number, one for the time, and one or more for the text of the subtitles
can you help?
\n{2,}
Split by this and you have your result.
This question already has answers here:
What is the best way to parse html in C#? [closed]
(15 answers)
Closed 9 years ago.
I have a long c# string of HTML code and I want to specifically extract bullet points "<ul><li></li></ul>".
Say I have the following HTML string.
var html = "<div class=ClassC441AA82DA8C5C23878D8>Here is a text that should be ignored.</div>This text should be ignored too<br><ul><li>* Need this one</li><li>Another bullet point I need</li><li>A bulletpoint again that I want</li><li>And this is the last bullet I want</li></ul><div>Ignore this line and text</div><p>Ignore this as well.</p>Text not important."
I need everything between the '<ul>' to '</ul>' tags. The '<ul>' tag can be excluded.
Now regular expression is not my strongest side, but if that can be used I need some help.
My code is in c#.
You should use the HtmlAgilityPack for things like this. I wrote a little introduction to it a while ago that may help you get going: http://colinmackay.scot/2011/03/22/a-quick-intro-to-the-html-agility-pack/