0

In IDEs a common operation is to perform a search and replace operation using a regex pattern in the search field and another pattern with capture groups in the replacement field.

For example, I can search for (\d)(\D)(\d) and replace it with first: \1; second: \2; third: \3. This would produce the following results.

  • "1a2" => "first: 1; second: a; third: 2"
  • "8o8" => "first: 8; second: o; third: 8"

Is there an easy way to perform this same operation on a String object in Apex? I'm looking for a solution that isn't hard-constrained to only process three capture groups. The number of capture groups is variable: could be 2, 3, 5 or any number.

0

There are Matcher and Pattern classes, that can be used to use regex. for example the following code does, that you want

String toCheck = '1a2';
Map<Integer, String> descriptionToGroupNumber = new Map<Integer, String>{
    1 => 'first',
    2 => 'second',
    3 => 'third',
    4 => 'fourth',
    5 => 'fifth'
};
Pattern pat = Pattern.compile('(\\d)(\\D)(\\d)');
Matcher match = pat.matcher(toCheck);
Integer groupsToCapture = match.groupCount();
while(match.find()){
    String output = '';
    for(Integer i = 1; i <= groupsToCapture; i++){
        if(i != 1){
           output +=' '; 
        }
        output +=descriptionToGroupNumber.get(i)+': '+ match.group(i);
        if(i != groupsToCapture){
           output+=';';
        }
    }
    System.debug(output);  
}

output is

DEBUG|first: 1; second: a; third: 2

\1 equals to capturing specific group match.group(1)

  • Thanks for the first answer, Alex. I'm looking for a solution that isn't hard-constrained to only process three capture groups. The number of capture groups is variable: could be 2, 3, 5 or any number. – Marty C. Jul 14 '17 at 13:56
  • Thank you also for pointing out the unclear requirement in my question. I've edited the question to explain that I'm seeking a dynamic solution. – Marty C. Jul 14 '17 at 13:57
  • @MartyC. I've updated my answer with case, when there could be unknown number of groups – Oleksandr Berehovskyi Jul 14 '17 at 14:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.