import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Example {
public static void main(String[] args) {
final String regex = "(?i)\\bhttps?[-\\w.\\~:/?#[\\]@!$&'()*+,;=]+|[@#](\\w+)|U\\+([A-F\\d]{5})";
final String string = "This is a @ping and a #hash.\n"
+ "This is a www.example.com, this is http://example.com?asdf=1234#anchor\n"
+ "https://www.example.net/a/b/c/?g=5&awesome=foobar# U+23232 http://www5.example.com\n"
+ "https://sub.sub.www.example.org/ @pong@pug#tagged\n"
+ "http://example.com/@dave\n"
+ "more http://example.com/more_(than)_one_(parens)\n"
+ "andU+98765more http://example.com/blah_(wikipedia)#cite-1\n"
+ "and more http://example.com/blah_(wikipedia)_blah#cite-1\n"
+ "and more http://example.com/(something)?after=parens";
final Pattern pattern = Pattern.compile(regex);
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