import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Example {
public static void main(String[] args) {
final String regex = "^(https?:\\/\\/)? #протокол\n"
+ "([\\w-]{1,32}\\.[\\w-]{1,32}) #домен\n"
+ "[^\\s@]* #любой не пробельный символ + @\n"
+ "$\n";
final String string = "/********************** yes *****************/\n"
+ "http://yan-dex.ru\n"
+ "http://asd-asd.ru\n"
+ "sdf-sdf.ru\n"
+ "http://sdfsdf.ru/sadasds-sadsad/sadsad\n"
+ "http://sdfsdf.ru/sadasds-sadsad/sadsad?page=2\n"
+ "http://sdfsdf.ru/sadasds-sadsad/sadsad?page=2&other=1\n"
+ "sdfsdf.ru/sadasds-sadsad/sadsad?page=2&other=1\n"
+ "http://sdfsdf.ru/sadasds/sadsad?page=2&other=1\n"
+ "https://regex101.com/r/tM7Pmr/1\n"
+ "http://regexpres.narod.ru/calculator.html\n"
+ "http://regexpres.narod.ru/calculator.htm\n"
+ "http://regexpres.narod.ru/calculator.php\n"
+ "http://regexpres.narod.ru/index.php\n"
+ "http://regexpres.narod.ru/index\n"
+ "https://jsfiddle.net/3b0khwhasd6/\n"
+ "https://lenta.ru/articles/2017/03/06/ruvlogs/\n"
+ "https://www.youtube.com/watch?v=PDxVcTWVIck\n"
+ "www.youtube.com/watch?v=PDxVcTWVIck\n\n"
+ "https://zakon.ru/blog/2017/03/06/delo_o_denezhnyh_generatorah_i_udobnom_sude__p-v_protiv_ikea_opredelenie_skgd_vs_ot_17012017_36-kg16\n"
+ "https://zakon.ru/konstitucionnyj_sud_rf\n\n"
+ "zakon.ru/blog/2017/03/06/delo_o_denezhnyh_generatorah_i_udobnom_sude__p-v_protiv_ikea_opredelenie_skgd_vs_ot_17012017_36-kg16\n"
+ "https://zakon.ru/konstitucionnyj_sud_rf\n\n\n"
+ "/********************** not *****************/\n"
+ "4234czxc\n"
+ "sdfsdf\n"
+ "sdsad.rturturturt/fsdfsd@sd";
final Pattern pattern = Pattern.compile(regex, Pattern.MULTILINE | Pattern.COMMENTS);
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