import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Example {
public static void main(String[] args) {
final String regex = "^((?|[1-9](?=.*1\\t+(?<x>0+))|[1-9]\\d(?=.*2\\t+(?<x>0+))|[1-9]\\d{2}(?=.*3\\t+(?<x>0+))|[1-9]\\d{3}(?=.*4\\t+(?<x>0+))|[1-9]\\d{4}(?=.*5\\t+(?<x>0+))|[1-9]\\d{5}(?=.*6\\t+(?<x>0+))|[1-9]\\d{6}(?=.*7\\t+(?<x>0+))|[1-9]\\d{7}(?=.*8\\t+(?<x>0+))))\\b";
final String string = "1\n"
+ "12\n"
+ "123\n"
+ "12345678\n"
+ "123456789\n\n"
+ "1 00000000\n"
+ "2 0000000\n"
+ "3 000000\n"
+ "4 00000\n"
+ "5 0000\n"
+ "6 000\n"
+ "7 00\n"
+ "8 0";
final String subst = "${x}$1";
final Pattern pattern = Pattern.compile(regex, Pattern.DOTALL | 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