Reputation: 3405
I am using Regex to return two lines of text, but it returns newlines with additional text.
//Text is located in txt file and contains
I've been meaning to talk with you.
I've been meaning to talk with you.
I've been meaning to talk with you.
string text = File.ReadAllText(@"C:\...\1.txt");
Regex unit = new Regex(@"(?<Text>.+(\r\n){1}.+)");
MatchCollection mc = unit.Matches(text);
foreach (Match m in mc)
foreach (Group g in m.Groups)
Console.WriteLine(g.Value);
Upvotes: 2
Views: 88
Reputation: 626738
You may use
var m = Regex.Match(text, @"^.+(?:\n.+)?");
if (m.Success)
{
Console.Write(m.Value.Trim());
}
Details
^
- start of string.+
- any 1+ chars other than an LF symbol(?:\n.+)?
- an optional sequence of:
\n
- a newline.+
- any 1+ chars other than an LF symbolThe .Trim()
is used here to trim the result from a possible CR symbol (since in .NET regex, .
also matches CR symbol.
Upvotes: 2