Split text file into multiline string array using Regex [duplicate] - c#

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.

Related

Regexp for decimal positive/ negative numbers [duplicate]

This question already has answers here:
Regex match entire words only
(7 answers)
Closed 4 months ago.
I'm having such string:
(Reasoncode1 -49.00)
From that string I need to take all the decimal values both positive/ negative so --> -49.00
I've created such regexp: -?\d+\.*\d*
It work but if the string has number inside like: "Reasoncode1" then it takes that one value and I want to avoid it.
Try this regex
-?[0-9]*\.[0-9]*

replace string between delimiters [duplicate]

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(",", "-"));

Replace (,) with (',') [duplicate]

This question already has answers here:
C# string replace does not actually replace the value in the string [duplicate]
(3 answers)
Closed 7 years ago.
I want to replace a single comma (,) with (',').
For example:
"text,text,text" with "text','text','text".
I tried
MyText.Replace(',',"','");
but can't get anything working properly.
Any help would be appreciated.
Try:
MyText = MyText.Replace(",","','");
There are two .Replace methods on strings. One for single characters and one for strings.
When you ',' that defines a single character so it goes to the single character version of the method. If you use double quotes then it defines a string so that version of the method is selected.
Docs on the string replace method: https://msdn.microsoft.com/en-us/library/system.string.replace(v=vs.110).aspx

How to ignore a character while Splitting a string in C# [duplicate]

This question already has answers here:
How to properly split a CSV using C# split() function?
(6 answers)
Closed 7 years ago.
I've a string like this
1,2,3,"a,b,c",4,"5,6"
I want split above string using .Split(',');
Expected:
1
2
3
a,b,c
4
5,6
Actual: As usual, it is splitting 4 and 6 also. It is default behaviour. But any other ways where can i get ehat i am expecting?
Try like this:
var result = Regex.Split(myString, ",(?=(?:[^\"]*\"[^\"]*\")*[^\"]*$)\");
REGEX DEMO

Splitting a comma delimited text file [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Reading csv file
I have a comma delimited file:
"Some Text, More Text", 1, 2, 3,4,5,6
"Random Text, text text", 2,4,5,6,7,8
var content = reader.ReadLine();
var stringArray = content.Split(',');
The problem is the text ends up being split into two parts. I want to keep it as one unit. So what are my options?
EDIT: I meant like
Some Text
More Text
1
2
3
4
5
6
I want it like
Some Text,More Text
1
2
3
4
5
6
How about finding all the matches of this regex:
"[^"]*"|\S+
I usually use the Microsoft.VisualBasic.FileIO.TextFieldParser object, see:
http://msdn.microsoft.com/en-us/library/f68t4563.aspx
and example of implementation at:
http://www.siccolo.com/Articles/CodeProject/Open_DataSet_From_TextFile/open_dataset_from_text_csv_file.html
This allows me to handle CSV files without worrying about how to cope with whether fields are enclosed in quotes, contain commas, escaped quotes etc.
You need to use Regex in Split, so that text in quotes are excluded!

Categories

Resources