// Collection of code snippets by Arne Vajhøj 
// (likely posted to comp.os.vms/INFO-VAX, INFO-TPU, MACRO32, eksperten.dk, newz.dk, LinkedIn or other place sometime between 1990 and now) 
import java.util.regex.Pattern;

public class RegexReplace {
    private static final String fmt = "((%s,){5})(%s)((,%s){2})";
    private static final String col1 = "[^,]*";
    private static final Pattern pat1 = Pattern.compile(String.format(fmt, col1, col1, col1));
    public static String reReplace1(String s) {
        return pat1.matcher(s).replaceAll("$1'Y'$4");
    }
    private static final String col2 = "(\\d+|(\"[^\"]*\")|('[^']*')|(NULL))";
    private static final Pattern pat2 = Pattern.compile(String.format(fmt, col2, col2, col2));
    public static String reReplace2(String s) {
        return pat2.matcher(s).replaceAll("$1'Y'$12");
    }
    public static void test(String s) {
        System.out.println(s);
        System.out.println(reReplace1(s));
        System.out.println(reReplace2(s));
    }
    public static void main(String[] args) {
        test("1,\"blaha\",NULL,'N','Y','N','N',NULL");
        test("2,\"blahb\",NULL,'Y','Y','Y','N',NULL");
        test("3,\"blahc\",NULL,'N','N','N','N',NULL");
        test("4,\"blahd,nasty\",NULL,'N','N','N','N',NULL");
    }
}
