I need to retrieve a substring from a text field in a flow. is it possible by using regex instead of apex ? If i try this regex i receive false as result ( boolean ), i should get true and then i would like to retrieve the string value.

String : __gtm_campaign_url":"https://test/en/test/?utm_source=ig&utm_medium=cpc&utm_campaign= test+%7C+PRP+test+%7C+DE&utm_term=en

Regex : REGEX($Record.UTM_Parameters__c, "utm_campaign=([^&]*)")

So the desired output should be : test+%7C+PRP+test+%7C+DE

4 Answers 4


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.


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

    /* 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{
    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};

The REGEX() formula function is only capable of returning a Boolean; it cannot return the matched string. It's primarily intended for use in Validation Rule formulas. The working formula in your answer is the correct and only method for extracting a substring of variable length based on searching for a token, in Salesforce formulas.


Using this formula i was able to do it, but my question was more about using REGEX.

FIND("{!StartString}", {!YourString}) + LEN("{!StartString}"),
FIND("{!EndString}", {!YourString}, FIND("{!StartString}", {!YourString})) - FIND("{!StartString}", {!YourString}) - LEN("{!StartString}"))

To provide a different option for working with a string that is known to be a URL and its parameters other than RegEx which can be challenging, consider instead using the PageReference class to parse the URL and give you easy access to the parameters contained in the string.

String s = '__gtm_campaign_url":"https://test/en/test/?utm_source=ig&utm_medium=cpc&utm_campaign=test+%7C+PRP+test+%7C+DE&utm_term=en';
s = s.removeStart('__gtm_campaign_url":"');

PageReference pr = new PageReference(s);
String utmCampaign = pr.getParameters().get('utm_campaign');

system.debug(utmCampaign); // test | PRP test | DE

as a bonus: the PageReference class handles url encoding and decoding for you!

You must log in to answer this question.

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