Regex Help Please!

Hello fellow Blazers! Looking for some ideas as to how would you go about this.

I am in Salesforce Lightning. I want Regex to look at the last name of a student, then based on the first letter of the last name, I want the snippet I insert to populate a name in a field that assigns a task to that user. Example:

Student Name: Oscar de la Hoya

Return the name below based on alpha-split:
Online VA Students (A-F): Reggis Metayer
Online VA Students (G-O): Dahly Jimenez
Online VA Students (P-Z): Vien Le Delgado

In the above example, based on the students name, Regex would need to return the name “Dahly Jimenez” because she is the person who works with students whose last name starts with letters “G” through “O”.

If the student name was Jason L. Bateman, then the name needing to be returned would be “Reggis Metayer”.

The unknown factor is how many words or characters make up the students full name. Some have just first and last name while others have a middle initial, or a multi word last name like “Neil Degrasse-Tyson”.

Hope this makes sense. Thank you!
-Brad

Hey @Brad_Hedinger
Does the below snippet solve the purpose?

{formtext: name=name; default=Oscar de la Hoya}
{parts=splitregex(trim(name), "[\s-]")}
{lastname=parts[count(parts)]; trim=yes}
Last name={=lastname}
{initial=split(lastname, "")[1]; trim=left}
Initial={=initial}
{if: testregex(initial, "[a-f]", "i"); trim=left}
{assignee="Reggis Metayer"; trim=left}
{elseif: testregex(initial, "[g-o]", "i"); trim=left}
{assignee="Dahly Jimenez"; trim=left}
{else: trim=left}
{assignee="Vien Le Delgado"; trim=left}
{endif: trim=left}
Assignee={=assignee}

You can reduce the lines, i added more lines so that the logic is easy to understand.

3 Likes

GLORIOUS! Thank you so much @VinodGubbala!!!

2 Likes

@VinodGubbala Hello there! Hope you are well. I have been trying to figure out a needed update to this snippet but I am hitting a wall. The change I need to make is to the alpha-split formula so the split is determined on more that 1 initial. The split now needs to be last names between the following:

A - DUF: Armand (AJ) Izzo

DUG - LAI: Reggis Metayer

LAJ - REV: Dahly Jimenez

REW - Z: Eleida Sanchez

Maybe it's 3 splits, then a join of all 3? I feel like it has to be simpler than that though.

Thank you for the help!

cute-emoji

2 Likes

No idea if this will be helpful or not Brad. I threw the question into ChatGPT

To extract the last names from the given list and assign them to case workers based on the specified criteria (A - DUF, DUG - LAI, LAJ - REV, and REW - Z), you can use the following regular expression:

A - DUF: ([A-DUF][a-zA-Z\s]+)
DUG - LAI: ([D-LAI][a-zA-Z\s]+)
LAJ - REV: ([L-R][a-zA-Z\s]+)
REW - Z: ([S-Z][a-zA-Z\s]+)

Here's a breakdown of how this regex works:

  1. A - DUF: ([A-DUF][a-zA-Z\s]+): This part matches last names starting with letters A to DUF and captures them in a capturing group.

  2. DUG - LAI: ([D-LAI][a-zA-Z\s]+): This part matches last names starting with letters DUG to LAI and captures them in a capturing group.

  3. LAJ - REV: ([L-R][a-zA-Z\s]+): This part matches last names starting with letters LAJ to REV and captures them in a capturing group.

  4. REW - Z: ([S-Z][a-zA-Z\s]+): This part matches last names starting with letters REW to Z and captures them in a capturing group.

You can use this regex pattern in your programming language of choice to extract the last names and then assign them to case workers based on the specified criteria.

1 Like

You rock @Santa_Laren! I will certainly try this out. Stay tuned!

1 Like

I met with @Gaurang_Tandon today to discuss this further. Through his direction and some experimentation, the following was landed on:

First & Last Name: {formtext: name=name}
{note: preview=no; trim=yes; insert=no}
{parts=splitregex(trim(name),"^\w+(\W+)")}
{lastname=parts[count(parts)]; trim=yes}
{chars=lower(substring(replaceregex(lastname, "\s", ""), 1, 3))}
{if: comparestrings(chars, "duf")<=0; trim=left}
{assignee="Armand Izzo"; trim=left}
{elseif: comparestrings(chars, "lai")<=0; trim=left}
{assignee="Reggis Metayer"; trim=left}
{elseif: comparestrings(chars, "rev")<=0; trim=left}
{assignee="Dahly Jimenez"; trim=left}
{elseif: comparestrings(chars, "z")<=0; trim=left}
{assignee="Eleida Sanchez"; trim=left}
{endif: trim=left}
{endnote}
Full Name: {=name}
Last name: {=lastname}
Assignee: {=assignee}

2 Likes