3
votes

I'm trying to figure out a regular expression on Ruby 1.8.7 for removing the thread from emails. For doing so I need to remove all content between mail boundaries that matches the thread pattern, for example, on Mac Mail I would need to remove the text in bold (sample HTML is simplified to avoid using a lot of space, real mails' HTML is far less succinct):

From: XXXX
... mail headers ...
Content-Type: multipart/alternative;
  boundary="Apple-Mail=_EFA7D6C2-C778-4C8E-AA13-C97DF1FA9036"
... more mail headers ...

--Apple-Mail=_EFA7D6C2-C778-4C8E-AA13-C97DF1FA9036
Content-Transfer-Encoding: quoted-printable
Content-Type: text/plain;
  charset=us-ascii

New comment added from Mac Mail

On 12/06/2012, at 12:51, [email protected] wrote:

> Thread
> text
> to be
> removed
>=20

--Apple-Mail=_EFA7D6C2-C778-4C8E-AA13-C97DF1FA9036
Content-Transfer-Encoding: quoted-printable
Content-Type: text/html;
  charset=us-ascii

<html>... lots of HTML...

<span>On 12/06/2012, at 12:51, [email protected] wrote:</span>

<span> Thread </span>
<span> text </span>
<span> to be </span>
<span> removed </span>
<span>=20 </span>
</html>=

--Apple-Mail=_EFA7D6C2-C778-4C8E-AA13-C97DF1FA9036--

The regular expression I thought would capture the required text is:

--Apple-Mail=_EFA7D6C2-C778-4C8E-AA13-C97DF1FA9036.+?(\bOn.+?)(?!--Apple-Mail=_EFA7D6C2-C778-4C8E-AA13-C97DF1FA9036)

But this is not working as is capturing from the boundary right until the first "On ".

2

2 Answers

1
votes

Ok, so the solution for this was pretty simple, I ended up with an expression like the following:

--Apple-Mail=_EFA7D6C2-C778-4C8E-AA13-C97DF1FA9036.+?(On \\d{0,2}[\\/\\-]\\d{0,2}[\\/\\-]\\d{0,4}.+?)--Apple-Mail=_EFA7D6C2-C778-4C8E-AA13-C97DF1FA9036

No need to perform a look-ahead/behind for this.

0
votes

Here are two regular expressions that will match the text. You can gsub out the appropriate match groups in each expression.

  1. /(^On \d\d\/\d\d\/\d{4}.*$\n(\n>.*$)*\n\n)/
  2. /(<span>On \d\d\/\d\d\/\d{4}.*<\/span>\n\n(<span>.*<\/span>\n)*)/

This should be a good start but could be improved to be more generic, but since we only know about one example, I've just written this to work on that!

References