If I'm reading your question accurately
Then this is a question about using REGEX()
to determine if you should bother executing the MID()
and FIND()
portion.
Regex in formulas is only for determining if your target string matches a given pattern or not. This is explained in the documentation on the REGEX() function
Compares a text field to a regular expression and returns TRUE if there is a match. Otherwise, it returns FALSE.
A regular expression is a string used to describe a format of a string according to certain syntax rules.
also
The Salesforce regular expression engine matches an entire string as opposed to searching for a match within a string. For example, if you are searching for the name Marc Benioff, use the regular expression, .*Marc Benioff.*
, to find a match in a string like the following:
According to Marc Benioff, the social enterprise increases customer success.
So I believe that the main issue you're facing is that second quote. Your regex needs to be able to match the entire string (not just a part of it).
Yes, it is possible via formula
The general form would look like
IF(
/* Since you can't get data out other than true/false, trying to */
/* use a capturing group does nothing for you */
/* The ".*" at the start and end are to ensure you can match the rest of the string */
/* The "&?" helps to keep "[^&]*" from being overly greedy (which may not */
/* be required) but still allows the utm parameter to be at the end of the string */
REGEX($Record.UTM_Parameters__c, ".*utm_campaign=[^&]*&?.*"),
< mess of MID(), FIND(), and LEN() here >,
/* we need to return something of type string when the regex doesn't match */
""
)
...but this would be much simpler in Apex, if you can stomach that
For anything more than simple string manipulation, Apex is going to be much easier to write, understand, and maintain.
public class InputWrapper{
@InvocableVariable
public String url;
}
@InvocableMethod(label='a' description='b')
public static List<String> getUTMCampaign(List<InputWrapper> wrappedInputList){
String testStr = wrappedInputList[0].url;
// In Apex, we can make use of the matching group
// The '$1' tells us to output the first matched group, and matching the entire
// string ensures that we _only_ output the matched group
String result = testStr.replaceFirst('.*utm_campaign=([^&]*)&?.*', '$1');
// Alternative approaches using other String class methods like split()
// or substring() are also possible
//String result = testStr.substringBetween('utm_campaign=', '&');
return new List<String>{testStr};
}