Match multiple lines with regex in C #

I have the following text:

--------------030805090908050805080502 Content-Type: image/jpeg Content-Transfer-Encoding: base64 Content-ID: < part16.07030906.00090703@highcontrast.ro > /9j/4AAQSkZJRgABAgAAZABkAAD/7AARRHVja3kAAQAEAAAARgAA/+4ADkFkb2JlAGTAAAAA /9j/4AAQSkZJRgABAgAAZABkAAD/7AARRHVja3kAAQAEAAAARgAA/+4ADkFkb2JlAGTAAAAA QBQH/9k= --------------030805090908050805080502 Content-Type: image/jpeg Content-Transfer-Encoding: base64 Content-ID: < part17.07010805.02020809@highcontrast.ro > /9j/4AAQSkZJRgABAgAAZABkAAD/7AARRHVja3kAAQAEAAAARgAA/+4ADkFkb2JlAGTAAAAA /9j/4AAQSkZJRgABAgAAZABkAAD/7AARRHVja3kAAQAEAAAARgAA/+4ADkFkb2JlAGTAAAAA juu41lRHFLufPCAID//Z --------------030805090908050805080502-- 

And I need to get with Regex in parts of C # 2:

  • between the first and second occurrence of the line "--------------030805090908050805080502"
  • between the lines "--------------030805090908050805080502" and "--------------030805090908050805080502--"

I tried this regex:

 --------------030805090908050805080502(\r.*)*--------------030805090908050805080502 

but in C # regex.Matches(...) only returns "--------------030805090908050805080502" .

+6
c # regex
source share
2 answers
 MatchCollection matches = Regex.Matches( text, @"([-]+\d{24}) (?<Content>.*?) (?=\1)", RegexOptions.IgnorePatternWhitespace | RegexOptions.Singleline ); foreach ( Match match in matches ) { Console.WriteLine( string.Format( "match: {0}\n\n", match.Groups[ "Content" ].Value ) ); } 

Update:. This expression will find all matches that occur between two occurrences of a number. If the number should be specific, and not any 24-digit number, change "\ d {24}" to the number you want to match.

+9
source share

Have you tried Split :

 var str = stringToParse.Split( new[] { "--------------030805090908050805080502" }, StringSplitOptions.None); Console.WriteLine(str[1]); 
+1
source share

All Articles