function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Steve ConnellySteve Connelly 

Need to extract portions of an email using formulas or regex

Working in Process Builder. I need to extract two pieces of asystem generated email and place them in fields using formulas or regex.

The email is generated by an external suystem and sent to Salesforce where it generates a case. The emails are structured similarly with only the username and account info cahnging from email to email. Note that the username from this external system will often be an email address but will not always be. The usernames in this external system do not have a direct correlation to users/contacts in Salesforce.

Here is the section of the email I am working with:

RSBSA login credentials We will call you soon to provide you with a temporary password. * Institution Name: ABC Corporation (12345) * Name: Smith Mary * Username: msmith@abccorporation.com<mailto:msmith@abccorporation.com> * User Role: Security Admin * E-mail: msmith@abccorporation.com<mailto:msmith@abccorporation.com>

The parts that I need to extract are the account ID (bold itallic - five digit number) and the username (in this example an email address).

In about 1/2 the cases the username will not be an email address and as such will not be followed by the "mailto" tag.

Since the account ID is preceded by an account name of unknown length and the username is of an unknown length, I am having a hard time isolating these bits.

Does anyone out there have any suggestions on how to approach this? It has been suggested that I use Apex to parse the whole email but that is just not in my wheelhouse so I am trying to find other ways to get this done.

Any suggestions?
Steve
Best Answer chosen by Steve Connelly
Alain CabonAlain Cabon
@Steve Connelly

You can use two regular expressions.

First made some tests here ( button : TEST MATCH ) :  https://www.freeformatter.com/java-regex-tester.html#ad-output

1) Java Regular Expression : Institution Name:.+?\(([0-9]+)\)

The idea is to get the group index 1 enclosed with parentheses after "Institution Name".

User-added image


User-added image


2) Java Regular Expression : Username: (.+?)[<| ]

The idea is to get the group index 1 after "Username" up to "<" or a space.

User-added image

If that is correct, the conversion in Apex is easy by just doubling the antislashes ( Institution Name:.+?\\(([0-9]+)\\)  )

Apex uses the same code that java for the regular expressions but the most important is to find the regular expressions that work.
It is some tries above.
 
Pattern r = Pattern.compile('Institution Name:.+?\\(([0-9]+)\\)'); 

Matcher rm = r.matcher('RSBSA login credentials We will call you soon to provide you with a temporary password. * Institution Name: ABC Corporation (12345) * Name: Smith Mary * Username: msmith@abccorporation.com<mailto:msmith@abccorporation.com> * User Role: Security Admin * E-mail: msmith@abccorporation.com<mailto:msmith@abccorporation.com>'); 

if(rm.find()) {
 system.debug('group 1:' + rm.group(1)); 
}