Expand my Community achievements bar.

Extract all the matched values in a given string to an array- AJO

Avatar

Level 4

Hi All,

Need help regarding for requirement- "Extract all the matched values within double curly braces and add all of those matched items to an array."

{% let InputString= "This is an input String-{{match1}} and need to extract all values inside double curly braces {{match2}}, {{match3}}" %}
{% let Output = regexGroup(InputString,"\\{\\{ *([^{}]+?) *\\}\\}", 1 ) %} 

Actual Output=  match1

Expected Output= ["match1","match2","match3"]

----------------------------------------------------------

{% let Output = regexGroup(InputString,"\\{\\{ *([^{}]+?) *\\}\\}", 1 ) %} - This only returns the first matched element and not the array of all the matched strings inside {{}}.

Is there a way to achieve an array as output and not just first encountered matched value?

 

Just to make the ask concise and straightforward, my question is not related to regex to find the matched ids BUT  A WAY TO RETURN MORE THAN ONE MATCHED VALUES IN FUNCTION -  regexGroup(string, string, group) , as of now it takes the group value and since i have provided 1 in group , it returns the first value , but i wanted to get ALL the matched values with any regex format.  Question is how to get and retrieve more than 1 matched values, may be with this function or by any other way?

2 Replies

Avatar

Community Advisor

Hello @Poonam_Dogra  Unfortunately the function doesn't work as per your expectation, it is purely based on grouping rather than matching.

Please read the fine print carefully : https://experienceleague.adobe.com/en/docs/journey-optimizer/using/content-management/personalizatio...

nnakirikanti_0-1741643971468.png

 

As a alternative you can do below approach if its fits in to your use case.

1) Use "split" to prep a list.

2) loop through the list items to match against the regex and append to a variable to prep the final string.

 

Let me know if this works for you.

 

`Cheers,

NN.

 

 

 

 

 

 

 

Avatar

Level 4

Thanks @nnakirikanti for responding and suggestions. Somehow i was not getting option to reply to your response, so i replied to question.

I was actually trying not to use split or any looping because my input string itself is too long and looping will make the performance bad, so i thought if there is any out of the box way, but as u suggested it doesn't seem to be a case, so i will try the looping, it should work , will update! Thank you!

Regards,

Poonam