0

I have a field to store the Latest Comments on a Case. For email responses I would only like to store the current email response and not all of the historic replies.
Most of my email responses are split with

'On ' + 'DDD, MMM dd, YYYY at HH:MM XM' + 'Sender Name' + '<' + 'Sender Email' + '> wrote:'

i.e On Tue, Apr 5, 2022 at 10:10 AM Kimble Support [email protected] wrote:

I am using the following code to locate the string and return the text before...

String trimmed_before;
            String trimRegExp = 'On ' + '(.+)' + '> wrote';
            Matcher m = Pattern.compile(trimRegExp).matcher(input);
            while(m.find()){
                Integer startPosition = m.start();
                    System.debug('startPosition = ' + startPosition);
                    trimmed_before =input.mid(0,startPosition);
                }
            if(trimmed_before==null){
                trimmed_before = input;
                }
                System.debug('trimmed_before = ' + trimmed_before);

However the "inner" portion of my Regex Pattern is too simplistic and returns anything starting with On and ending with wrote. How can I adjust the Pattern to match the format - 'On ' + 'DDD, MMM dd, YYYY....

1 Answer 1

0
String trimRegExp = 'On ' + '(.+)' + '> wrote';

Should be something more like:

String trimRegExp = '(?m)^ *On (.+?) wrote: *$';

Where (?m) enables "multiline mode", where ^ matches the start of a line and $ matches the end of a line. Normally, these operators only work on the first and last character of input, respectively.

Also, you can just use String.split for this purpose:

String trimRegExp = '(?m)^ *On (.+?) wrote: *$';
String message = input.split(trimRegExp,2)[0];

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .