import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Example {
public static void main(String[] args) {
final String regex = "(?=.*(?:epub|digital|e-?(?:ISBN|book))).*ISBN(?: *13)?:?\\s+\\K(\\d(?:-?\\d){12})";
final String string = "These should match to indicate they are eBook ISBNs:\n"
+ "ISBN 978-1-9821-8584-8 (ebook)\n"
+ "EISBN: 9781101487280\n"
+ "eISBN: 978-1-4406-3132-0\n"
+ "eISBN 9781429902304\n"
+ "Ebook ISBN: 9780593547342\n"
+ "eBook ISBN: 9780804139038\n"
+ "e-ISBN 978-0-765-37686-2\n"
+ "Digital Edition MAY 2017 ISBN: 978-0-06-244198-0\n"
+ "ISBN 13: 978-0-9999999-9-9 (eBook edition)\n"
+ "EPub © Edition JUNE 2003 ISBN: 9780061739200\n\n\n"
+ "But then these should not match, as they are assumbed to be print copy ISBNs:\n"
+ "Print ISBN: 978-0-06-244197-3\n"
+ "ISBN 978-1-9821-8582-4\n"
+ "ISBN 9780804139021\n"
+ "ISBN-13: 978-1-4299-6030-4\n"
+ "ISBN 13: 978-0-9999999-9-9 (Paperback edition)";
final Pattern pattern = Pattern.compile(regex, Pattern.MULTILINE | Pattern.CASE_INSENSITIVE);
final Matcher matcher = pattern.matcher(string);
while (matcher.find()) {
System.out.println("Full match: " + matcher.group(0));
for (int i = 1; i <= matcher.groupCount(); i++) {
System.out.println("Group " + i + ": " + matcher.group(i));
}
}
}
}
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