import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Example {
public static void main(String[] args) {
final String regex = "(?(DEFINE)(?<urlPart>[^\\\\\\s]+))\\\\\\\\(?&urlPart)\\\\(?&urlPart)\\\\\\K(?&urlPart)";
final String string = "### Positive matches\n"
+ "\\\\10.20.3.23\\S$\\COMPANY_NAME\\Main_5e08a942f39a430db0b081736a3f1881\\C_VOL-b002.spf\n"
+ "\\\\localhost\\part1\\selectMe\n"
+ "\\\\a\\b\\select-me but not me\n"
+ "\\\\a\\b\\select_me\\c\\d\\e.abc more text here\n"
+ "This is the link \\\\a\\b\\select-me\\c\\d\\e.abc inside a longer string\n"
+ "\\\\a\\b\\select_me_from_1st_link\\c.abc \\\\a\\b\\selectMeFrom2ndLink\\c.abc\n\n"
+ "## Negative matches\n"
+ "\\a\\b\\c\\dontSelectMe\\e\\f\n"
+ "\\\\\\\\dont-select-me or me\n"
+ "\\\\a \\b \\dont_select_me \\c \\d \\e.abc";
final Pattern pattern = Pattern.compile(regex, Pattern.MULTILINE);
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