import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Example {
public static void main(String[] args) {
final String regex = "^(\\w+)\\sq\\[(\\d+)\\]((,)q\\[(\\d+)\\])*;$";
final String string = "qreg q[16],q[15];\n"
+ "x q[0];\n"
+ "cx q[4],q[0];\n"
+ "h q[0];\n"
+ "t q[1];\n"
+ "t q[5];\n"
+ "t q[0];\n"
+ "cx q[5],q[1];\n"
+ "cx q[0],q[5];\n"
+ "cx q[1],q[0];\n"
+ "tdg q[5];\n"
+ "cx q[1],q[5];\n"
+ "tdg q[1];\n"
+ "tdg q[5];\n";
final String subst = "k.gate(\"\\1\",\\2\\4\\5)";
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);
}
}
Please keep in mind that these code samples are automatically generated and are not guaranteed to work. If you find any syntax errors, feel free to submit a bug report. For a full regex reference for Java, please visit: https://docs.oracle.com/javase/7/docs/api/java/util/regex/Pattern.html