import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Example {
public static void main(String[] args) {
final String regex = "( # start capture url\n"
+ "(?:(?:(?:ht|f)tps?\\:)\\/\\/?|(?:www\\.)) # start with http or www \n"
+ "(?:\\s?[:\\w?=%&+-]+ # need some chars in url, allow spaces.\n"
+ "(?:(?:(?:\\.)|(?:\\/(?:\\/)?)))? # can have ., / or // inside urls.\n"
+ ")+ # allow repeat alternating pattern of chars and delimiters\n"
+ ") # end capture url";
final String string = "http://www.apple.com/legal/ privacy/.\n\n"
+ "abbbbbbbbbbbb\n"
+ "abababab\n"
+ "aaab\n"
+ "hi\n"
+ "h123213i.what\n\n\n"
+ "let's do some URLs next segment> \n"
+ "https://duckduckgo.com/?q=perl+regex+%2Fmxsp&t=canonical&atb=v143-1&ia=web, http://www.rexegg.com/regex-modifiers.html\n"
+ " <segment hi there segment> https://duckduckgo.com/?q=tutorial+perl+regex+substitution+capture+groups&t=canonical&atb=v143-1&ia=web-https://regex101.com/, www.giggle.com\n\n"
+ "B. Privacy Policy. At all times your information will be treated in accordance with Appleās Privacy Policy, which is incorporated by reference into this License and \n"
+ "can be viewed at: http://www.apple.com/legal/ privacy/. qweqweqwewqe\n\n"
+ "www.howdy.com/welcome::&5";
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