Regex to remove everything after gmail reply string

Viewed 84
This is my reply

On Wed, Jun 23, 2021 at 3:22 PM A User (User) <user@mygmailaccount.com>
wrote:
> hello this is my initial email.
>
>

I only want to be able to display my reply message and remove all previous responses.

I use the following regex expression, and it removes everything after "wrote:"

\wrote.*$

Which displays

This is my reply

On Wed, Jun 23, 2021 at 3:22 PM A User (User) <user@mygmailaccount.com>

I need an expression that will look for the "On" and "wrote:" and remove everything after it.

2 Answers

You could make the dot match a newline using for example an inline modifier (?s) and then first match On at the start of the string, and then match wrote: after matching a newline.

(?s)^\s*\bOn\b.*\nwrote:.*$

Regex demo

Or using a character class to match any char

^\s*\bOn\b[\s\S]*\nwrote:[\s\S]*$

Regex demo

This regex works for me in python:

r'\s*\bOn\b.*wrote:.[\s\S]*$'
Related