Reputation: 1353
Here is my string:
1-1 This is my first string. 1-2 This is my second string. 1-3 This is my third string.
How can I break like in C# like;
result[0] = This is my first string.
result[1] = This is my second string.
result[2] = This is my third string.
Upvotes: 2
Views: 331
Reputation: 35146
IEnumerable<string> lines = Regex.Split(text, "(?:^|[\r\n]+)[0-9-]+ ").Skip(1);
EDIT: If you want the result in an array you can do string[] result = lines.ToArray()
;
Upvotes: 5
Reputation: 111920
Regex regex = new Regex("^(?:[0-9]+-[0-9]+ )(.*?)$", RegexOptions.Multiline);
var str = "1-1 This is my first string.\n1-2 This is my second string.\n1-3 This is my third string.";
var matches = regex.Matches(str);
List<string> strings = matches.Cast<Match>().Select(p => p.Groups[1].Value).ToList();
foreach (var s in strings)
{
Console.WriteLine(s);
}
We use a multiline Regex, so that ^
and $
are the beginning and end of the line. We skip one or more numbers, a -
, one or more numbers and a space (?:[0-9]+-[0-9]+ )
. We lazily (*?
) take everything (.
) else until the end of the line (.*?)$
, lazily so that the end of the line $
is more "important" than any character .
Then we put the matches in a List<string>
using Linq.
Upvotes: 2
Reputation: 7282
Lines will end with newline, carriage-return or both, This splits the string into lines with all line-endings.
using System.Text.RegularExpressions;
...
var lines = Regex.Split( input, "[\r\n]+" );
Then you can do what you want with each line.
var words = Regex.Split( line[i], "\s" );
Upvotes: 0