Click here to Skip to main content
15,878,871 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I am reading the headers of an email message where the headers look like this:

Date: 24 Apr 2023 09:43:44 +0200
Subject: HELP! Location 'Name249fc8e9-068b-47c3-a39c-a3e4cdfe2c16' is not
 responding!
Content-Type: text/plain; charset=us-ascii
Content-Transfer-Encoding: quoted-printable

As you can see subject has a new line character, which needs to be a part of the subject header within my regex.

What I have tried:

The regex pattern
Quote:
.*?:\s.*
matches almost all of it, but the subject will miss the " responding!" part. How can I add the rest of the subject part and still get everything else.

I tried looking into lookarounds, but I dont know if this is the right way to do it. Any suggestions?
Posted
Updated 24-Apr-23 1:06am
v2
Comments
Richard Deeming 24-Apr-23 5:09am    
Rather than reinventing the wheel, why not use the battle-tested library that Microsoft recommend: MimeKit[^].
Evilfish2000 24-Apr-23 6:45am    
Could you post that comment as a solution cause this is just what I needed and more.
PIEBALDconsult 24-Apr-23 9:31am    
The dot doesn't match linefeeds by default. A bit inconvenient that it does match carriage return. Use the Singleline option.

Specifies single-line mode. Changes the meaning of the dot (.) so it matches every character (instead of every character except \n).

1 solution

As discussed in the comments, Microsoft officially recommend using MimeKit[^] for all email-related code.

It's free and open-source, and it's actively maintained:
GitHub - jstedfast/MimeKit: A .NET MIME creation and parser library with support for S/MIME, PGP, DKIM, TNEF and Unix mbox spools.[^]
 
Share this answer
 
Comments
PIEBALDconsult 24-Apr-23 10:13am    
That may be what the OP needs, but it doesn't answer the question he asked.
Richard Deeming 24-Apr-23 10:17am    
Which is why I originally posted it as a comment. The OP then asked me to post it as a solution, since it solves his real question, rather than the question he asked. :)
Evilfish2000 24-Apr-23 10:47am    
It's true that the answer is not strictly correct with the answer. But, I was trying to read the headers of emails and the meme kit does exactly that.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900