import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Example {
public static void main(String[] args) {
final String regex = "(?'group1'[\\d])";
final String string = "101011010\n"
+ "110101101\n"
+ "111010110\n"
+ "111101011\n"
+ "011110101\n"
+ "101111010\n"
+ "110111101\n"
+ "011011110\n"
+ "001101111\n"
+ "100110111\n"
+ "110011011\n"
+ "111001101\n"
+ "111100110\n"
+ "011110011\n"
+ "001111001\n"
+ "100111100\n"
+ "010011110\n"
+ "001001111\n"
+ "100100111\n"
+ "010010011\n"
+ "001001001\n"
+ "000100100\n"
+ "000010010\n"
+ "000001001\n"
+ "000000100\n"
+ "000000010\n"
+ "100000001";
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);
}
}
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