views:

143

answers:

3

I have the following text:

--------------030805090908050805080502
Content-Type: image/jpeg
Content-Transfer-Encoding: base64
Content-ID: <[email protected]>

/9j/4AAQSkZJRgABAgAAZABkAAD/7AARRHVja3kAAQAEAAAARgAA/+4ADkFkb2JlAGTAAAAA
/9j/4AAQSkZJRgABAgAAZABkAAD/7AARRHVja3kAAQAEAAAARgAA/+4ADkFkb2JlAGTAAAAA
QBQH/9k=
--------------030805090908050805080502
Content-Type: image/jpeg
Content-Transfer-Encoding: base64
Content-ID: <[email protected]>

/9j/4AAQSkZJRgABAgAAZABkAAD/7AARRHVja3kAAQAEAAAARgAA/+4ADkFkb2JlAGTAAAAA
/9j/4AAQSkZJRgABAgAAZABkAAD/7AARRHVja3kAAQAEAAAARgAA/+4ADkFkb2JlAGTAAAAA
juu41lRHFLufPCAID//Z
--------------030805090908050805080502--

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

  1. between the first and the second occurence of the string "--------------030805090908050805080502"
  2. between the strings "--------------030805090908050805080502" and "--------------030805090908050805080502--"

I tried this regex:

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

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

Any idee?

Thanks.

A: 

Regex can do Multiline:

var regex = new Regex([Your pattern here], RegexOptions.Multiline);

Rob Rodi
It doesn't work with Multiple. I've tried.
Emanuel
+1  A: 

Have you tried a Split:

var str = stringToParse.Split(
    new[] { "--------------030805090908050805080502" }, 
    StringSplitOptions.None);
Console.WriteLine(str[1]);
Darin Dimitrov
At this time this operation is done with split, but I want to use Regex.
Emanuel
+4  A: 
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 come between two occurrences of a number. If the number needs to be a specific one, rather than any 24-digit number, change "\d{24}" to the number you want to match.

Jeff Hillman
How your Regex will look like if I don't know the iteration number for "--------------030805090908050805080502" ?
Emanuel
Replace the iteration number with \d+ to match any string of digits. If the iteration number is always 24 digits, \d{24} will do it.
Jeff Hillman
I wanted to say that I don't know the number of lines in which "--------------030805090908050805080502" appear.
Emanuel