How can I successfully parse the text below in that format to parse just
To: User <test@test.com>
and
To: <test@test.com>
When I try to parse the text below with
/To:.*<[A-Z0-9._+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}>/mi
It grabs
Message-ID <CC2E81A5.6B9%test@test.com>,
which I dont want in my answer.
I have tried using $ and \z and neither work. What am I doing wrong?
Information to parse
To: User <test@test.com> Message-ID <CC2E81A5.6B9%test@test.com>
To:
<test@test.com>
This is my parsing information in Rubular http://rubular.com/r/DQMQC4TQLV
Since you haven’t specified exactly what your tool/language is, assumptions must be made.
In general regex pattern matching tends to be aggressive, matching the longest possible pattern. Your pattern starts off with
.*, which means that you’re going to match the longest possible string that ENDS WITH the remainder of your pattern<[A-Z0-9._+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}>, which was matched with<CC2E81A5.6B9%test@test.com>from theMessage-ID.Both Apalala’s and nhahtdh’s comments give you something to try. Avoid the all-inclusive
.*at the start and use something that’s a bit more specific: match leading spaces, or match anything EXCEPT the first part of what you’re really interested in.