user3739007
user3739007

Reputation: 3

Regex to get line content

I want to get a line content between "From:" until "\r\n", I heve tried this:

    string texto = @"From: .... blabla bla
    Message: blablabalab

    //linha em branco
    From: .... blabla bla
    Message: blablabalab

    //linha em Branco
    From: .... blabla bla
    Message: blablabalab";

   string[] lines = Regex.Split(texto, "From:\\s+\\rn");

thanks

Upvotes: 0

Views: 515

Answers (2)

Jakub Formela
Jakub Formela

Reputation: 81

You should use "From:(.*)$" $ is a default sign for line ending in C# regex engine.

You can read more about Regex in C# here

Upvotes: 0

Abion47
Abion47

Reputation: 24641

The pattern you're looking for is @"From:(.*?)\r\n". Also, you'll want to use Regex.Match rather than Regex.Split:

string from = Regex.Match(s, @"From:(.*?)\r\n").Value;

You can also use Regex.Matches if you want to get all the "from"s:

string[] froms = Regex.Matches(s, @"From:(.*?)\r\n")
                      .Cast<Match>()
                      .Select(m => m.Value)
                      .ToArray();

Upvotes: 2

Related Questions