简体   繁体   中英

Extract a text from a file c#

I got a file .mail that contains:

`

FromFild=xxx@gmail.com
ToFild=yyy@gmai.com
SubjectFild=Test
Message=
<b><font size="3" color="blue">testing</font> </b>
<table>
<tr>
    <th>Question</th>
    <th>Answer</th>
    <th>Correct?</th>
</tr>
<tr>
    <td>What is the capital of Burundi?</td>
    <td>Bujumburra</td>
    <td>Yes</td>
</tr>
<tr>
    <td>What is the capital of France?</td>
    <td>F</td>
    <td>Erm... sort of</td>
</tr>
</table>
  Message=END

 #at least one empty line needed at the end!

`

And i need to extract and save only the text that is between Message= and Message=END. I tried with split('=').Last/First(). Not good.I can not use Substring, as it accepts only int ofIndex. I am noob and i can not think of a sollution. Can you give a hint, please?

You can use this Regular Expression :

/Message=(?<messagebody>(.*))Message=END/s

Then the code to get message :

string fileContent; //The content of your .mail file
MatchCollection match = Regex.Matches(fileContent, "/Message=(?<messagebody>(.*))Message=END/s");
string message = match[0].Groups["messagebody"].Value;

I will assume that there is no constant number of lines in the text file or in the message your'e looking for that I can rely on.

        string prefix = "Message=";
        string postfix = "Message=END";

        var text = File.ReadAllText("a.txt");
        var messageStart = text.IndexOf(prefix) + prefix.Length;
        var messageStop = text.IndexOf(postfix);
        var result = text.Substring(messageStart, messageStop - messageStart);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM