import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Example {
public static void main(String[] args) {
final String regex = "^.*?([0-9]+-[a-zA-Z]{2,}-[0-9]+).*";
final String string = "\"NE-390-SM-04\"\n"
+ " --> \"390-SM-04\"\n\n"
+ "\"90055-SL-01-J\"\n"
+ " --> \"90055-SL-01\"\n\n"
+ "\"NE-1478-SL-02\"\n"
+ " --> \"1478-SL-02\"\n\n"
+ "\"87007-QM-01-J\"\n"
+ " --> \"87007-QM-01\"\n\n"
+ "\"NE-9315-BM-01-A\"\n"
+ " --> \"9315-BM-01\"\n\n"
+ "\"3121-SP-01\"\n"
+ " --> \"3121-SP-01\"\n\n"
+ "\"1639-YL-01\"\n"
+ " --> \"1639-YL-01\"\n\n"
+ "\"NE-9922-WM-01-J\"\n"
+ " --> \"9922-WM-01\"\n\n"
+ "\"ND-2498-YL-01\"\n"
+ " --> \"2498-YL-01\"\n\n"
+ "\"C-4008-PP-03-J\"\n"
+ " --> \"4008-PP-03\"\n\n"
+ "\"876-C-4008-PP-03-J-234\"\n"
+ " --> \"4008-PP-03\"";
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