import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Example {
public static void main(String[] args) {
final String regex = "(?:^|\\ |\\n)(((?:(?:http(?:s)?\\:)?(?:\\/\\/))|(?:\\/\\/))?(?:(?=[a-z0-9\\-\\.]{1,255}(?=\\/|\\ |$|\\:|\\n|\\?|\\,|\\!))(?:(?:(?:[a-z0-9]{1}(?:[a-z0-9\\-]{1,62})?\\.){1,127})[a-z]{2,}(?:\\.[a-z]{2})?))(?:[a-z0-9\\/\\-\\_\\%\\?\\&\\!\\$\\'\\,\\(\\)\\*\\.\\+\\=\\;])*?)(?=$|\\.(?=\\ |$)|\\:|\\n|\\ |\\?(?=\\ |$)|\\,|\\!)";
final String string = "I really like to go to google.com and google stuff.\n"
+ " <br>\n"
+ " I also like http://hercdev.io, both are pretty cool!\n"
+ "http://google.com\n"
+ " <br>\n"
+ " This is the url to an image: https://objects.dreamhost.com/herc-scripts/chicken_sombrero.gif\n"
+ " <br>\n"
+ " But this is the actual image: <img src=\"https://objects.dreamhost.com/herc-scripts/chicken_sombrero.gif\" style=\"max-width: 100px;\" />\n"
+ " <br>\n"
+ " Note that the url to the image in the <img> tag is unaffected. This is true for things that are already links also, like <a href=\"http://google.com\" target=\"_blank\">this.</a>";
final String subst = "<a href=\"$1\">$1</a>";
final Pattern pattern = Pattern.compile(regex, Pattern.CASE_INSENSITIVE);
final Matcher matcher = pattern.matcher(string);
// The substituted value will be contained in the result variable
final String result = matcher.replaceAll(subst);
System.out.println("Substitution result: " + result);
}
}
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