Question

5
Replies
7622
Views
BryanC97 Member since 2017 4 posts
Infosys Limited
Posted: February 13, 2018
Last activity: March 1, 2018
Closed
Solved

Regular Expression to extract a substring?

Here's the problem:

You have an arbitrary length string (it could be a 2000 character string or more) that you have to extract a substring from. The easiest way I know is using RegEx. The hardest way I know is using an activity with when/loops.

I'm not too particularly fond of creating a whole new activity with different when/loops when I have to look for a completely different substring, or when I'm looking for the same substring in a completely different formatted string.

The only out-of-the-box RegEx I can find in 7.1.x is from String (Pega-RulesEngine) in expression builder, however they don't extract substrings with RegEx.

so I'm left with using a Java step with the code below.

String mydata = tools.getParamValue("Input");
java.util.regex.Pattern pattern = java.util.regex.Pattern.compile(tools.getParamValue("Regex"));
java.util.regex.Matcher matcher = pattern.matcher(mydata);
if (matcher.find())
{
      tools.putParamValue("Output" ,PropertyInfo.TYPE_TEXT, matcher.group(0));
}

So basically you just put this java step in an activity, and have the string parameters: Input, Regex, Output.

You do property set on Input and Regex, and extract a string to Output by running the activity.

Any features like this in Pega without using Java steps?

On a side-note: The projects I'm working is migrating to 7.3.1 soon.

Low-Code App Development Data Integration Java and Activities
Moderation Team has archived post
Share this page LinkedIn