3

I have an email service that parses an email and inserts or updates a set of fields.

I have trawled through previous questions and can see that many regex posts are voted down for being not 'Salesforce' enough but I have tested this everywhere: https://regex101.com/ and the formula works just not in Salesforce:

The data below is a pretend email I may receive:

Job Title: Project Manager
First Name: Alpha
Last Name: Tester

This may also be:

Job Title: Trawler
First Name: Better
Last Name: Tester

My regex is:

(?<=Job Title: ).*$

Which returns: Both Trawler and Project Manager when processed at regex101

In Salesforce I get:

Insert failed. First exception on row 0; first error: REQUIRED_FIELD_MISSING, Required fields are missing: [Job Title]: [Job Title]

Because it matches nothing in the email.

I found this post: Regex: Make Dot Match Newline? and it was quite helpful and mentions that Salesforce implements a different 'end-of-line' code. When I look at https://docs.oracle.com/javase/7/docs/api/java/util/regex/Pattern.html is doesn't seem to make any such suggestion.

Clearly I'm being stupid and just cannot see the answer so:

How can I complete this regex in Apex to make this match 'All characters from the known item to the end of the line, no matter how many words, characters or white space characters'?

edit: adding apex code

Called from here:

myPlainText = email.plainTextBody;
mySubject = email.subject;
newthing.field__c = getItemFromString('Job Title: ', myPlainText);

Separate method:

    when 'Job Title: '{
        //MyPattern = Pattern.compile('Job Title: (\\S+)\\s');
        //MyPattern = Pattern.compile('Job Title: .*$');
        MyPattern = Pattern.compile('(?<=Job Title: ).*$');
        MyMatcher = MyPattern.matcher(pEmailBody);
        if(MyMatcher.find()){
            response = MyMatcher.group(0);
        }
    }
  • 1
    Can you edit your question to include the portion of your apex that builds/uses the regex? The regex is correct, but you may be getting tripped up on find() vs matches() – Derek F Jan 21 '19 at 16:18
  • @DerekF added code – SeanGorman Jan 21 '19 at 16:36
  • The code you have included is a bit incomplete to understand where you are going awry. – Adrian Larson Jan 21 '19 at 16:58
5

You need to turn on multi-line mode in order for the $ specifier to match at a line terminator (rather than at the end of the string). Multi-line mode is activated with the token (?m).

As Derek F. alluded to in a comment, you'll also need to use the find() method (rather than matches()) in order to identify a substring match.

Here's a sample using your regex, with this modification, that debugs "Project Manager":

String test = 'Job Title: Project Manager\nFirst Name: Alpha\nLast Name: Tester';
Pattern regex = Pattern.compile('(?m)(?<=Job Title: ).*$');
Matcher regexMatcher = regex.matcher(test);

while (regexMatcher.find() == true) {
    System.debug(regexMatcher.group());
}
|improve this answer|||||
  • I added my code above. – SeanGorman Jan 21 '19 at 16:42
  • This did it. I had to remove a line I was using to make up for the fact I couldn't find a way to remove the key. The (?m)(?<=key) really helped. thank you – SeanGorman Jan 21 '19 at 17:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.