import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Example {
public static void main(String[] args) {
final String regex = "_(?:(?!(?:.*?\\d{6}))|(?=[^\\d]+\\d{8}))";
final String string = "Patate_17890505_TitreEnCamelCase.ext\n"
+ "EPFL_AlgebreLineaire\n"
+ "TaxonomieProprietesFonctionsComplexes.v0.ipe.20210302_005606.pdf\n"
+ "1_\n"
+ "12_\n"
+ "_1\n"
+ "_12\n"
+ "12345678_\n"
+ "_123456\n"
+ "12345678_12345\n"
+ "1234567_123456\n"
+ "1234567_12345\n"
+ "123456_12345\n"
+ "12345678_1234567\n"
+ "123456789_123456\n"
+ "123456789_1234567\n"
+ "_patate__truc__\n"
+ "___\n"
+ "foo_12345678\n"
+ "foo_12345678_123456_bar\n"
+ "12345678_123456\n"
+ "foo12345678_123456bar";
final String subst = ".";
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