-
Notifications
You must be signed in to change notification settings - Fork 0
/
RegexReplacer.cls
41 lines (31 loc) · 1.58 KB
/
RegexReplacer.cls
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
public class RegexReplacer {
// Declaring a class to handle the inputs
public class ReplacementRequest {
@InvocableVariable(label='Input String' required=true)
public String inputString;
@InvocableVariable(label='Regex Pattern' required=true)
public String regexPattern;
@InvocableVariable(label='Replacement Value' required=false) // Not required. If left blank, input string will be removed.
public String replacementValue;
}
// Declaring a class to handle the outputs
public class ReplacementResult {
@InvocableVariable(label='Output String')
public String outputString;
}
@InvocableMethod(label='Replace String via Regex')
public static List<ReplacementResult> replaceText(List<ReplacementRequest> requests) {
List<ReplacementResult> results = new List<ReplacementResult>();
for (ReplacementRequest request : requests) {
// Create a new instance of the output object
ReplacementResult result = new ReplacementResult();
// Handling null replacementValue by defaulting to empty string
String actualReplacementValue = (request.replacementValue != null) ? request.replacementValue : '';
// Regex replace operation with the adjusted replacement value
result.outputString = request.inputString.replaceAll(request.regexPattern, actualReplacementValue);
// Add the result to the list
results.add(result);
}
return results;
}
}