import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Example {
public static void main(String[] args) {
final String regex = "https?:\\/\\/(?:w{1,3}\\.)?[^\\s.]*(?:\\.[a-z]+)+(?::\\d+)?(?![^<]*(?:<\\/\\w+>|\\/?>))";
final String string = "This is my text to be parsed which contains url \n"
+ "http://someurl.com <a href=\"http://thisshouldnotbetampered.com\">\n"
+ "some text and a url http://someotherurl.com test 1q2w </a> <img src=\"http://someasseturl.com/abc.jpeg\"/>\n\n"
+ "Hello http://someurl.com</a> <!-- fail -->\n"
+ "<img src=\"http://someurl.com/image.jpg>\n"
+ "http://someurl.localhost.com\n"
+ "<div>Regex is awesome</div>\n\n\n\n"
+ "<a href='https://help.mojohelpdesk.com/mytickets/show/26737327'>https://help.mojohelpdesk.com/mytickets/show/26737327</a>\n\n"
+ "<a href=\"https://help.mojohelpdesk.com/mytickets/show/26737327\">https://help.mojohelpdesk.com/mytickets/show/26737327</a>\n\n"
+ "https://google.com\n\n"
+ "https://www.mojo.com\n\n\n\n"
+ "http://localhost:3000\n\n"
+ "http://site.quelqu\n\n"
+ "http://localhost.com:3000\n\n"
+ "www.google.com";
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