In one replaceFirst-activity: Replace ABC: by ABC: and DEF: by DEF:

Got this text: BEGIN:VCALENDAR VERSION:2.0 PRODID:-//ABC Corporation//NONSGML My Product//EN BEGIN:VTODO DTSTAMP:19980130T134500Z SEQUENCE:2etc... and want to turn this into json. Got it done using a replace-activity per line: replaceFirst($EventJson,'DTSTAMP:','DTSTAMP":"') This takes a lot of replace-activities and is error-prone and laborious. Is there a possibility to do this in one replace-statement? It would need to search for (?’field’[A-Z]+): and replace it with the found string and wrapped in double quotes and a double quote before the : Something like this: replaceFirst($EventJson,'(?'field'[A-Z]+):', '"field":"') I have created a Java-action for it, and that works, but would rather see this being possible in Mendix.
2 answers

I would use replaceAll to change the : into “:”


Then I would put double quotes around each line:

I would do a repalceAll (‘(.+)’ , ‘”$1”’) but it is not working (this link shows it could)


Maybe a Java Action:

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Example {
    public static void main(String[] args) {
        final String regex = "(.+)";
        final String string = "BEGIN:VCALENDAR\n"
     + "VERSION:2.0\n"
     + "PRODID:-//ABC Corporation//NONSGML My Product//EN\n"
     + "BEGIN:VTODO\n"
     + "DTSTAMP:19980130T134500Z\n"
     + "SEQUENCE:2etc...";
        final String subst = "\"$1\"";
        final Pattern pattern = Pattern.compile(regex, Pattern.MULTILINE);
        final Matcher matcher = pattern.matcher(string);
        // The substituted value will be contained in the result variable
        final String result = matcher.replaceAll(subst);
        System.out.println("Substitution result: " + result);





I have used the RegexReplaceAll from the Community Commons module when I’ve had to do more complex multiline replacements like this. It’s a lot more powerful than the built in version. However, it is still dropping out to Java and I know you wanted it in standard Mendix.