import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Example {
public static void main(String[] args) {
final String regex = "(?<=profile\\.php\\?id\\=)(?<id>[0-9]+)|(?:(?<=\\.com)|(?<=\\.me)|(?<=\\.co)|(?<=\\.us))(?:(?:\\/groups\\/|\\/)(?!profile\\.php)(?<username>[\\w\\.\\_]+))";
final String string = "https://www.facebook.com/hung.le054\n"
+ "https://www.facebook.com/groups/j2team.community/?ref=nf_target&fref=nf&__xts__%5B0%5D=68.ARCufAkDYLfgBw09Ctl9HKUd8wMYIaATKYHE-9guWWpCGu86iH8k0p4Qx8BxlXEEJMW8shzzodIckx0qvnBg94kwMe3uOIPnjE9s6neoXwFX8kBJsWBpF-Ej8oXpGdiIu6sf9cg9tyeq&__tn__=C-R\n"
+ "https://www.facebook.com/NoExit.GoEXID/?hc_ref=ARQCFBLI4l-ciKCPEGazrn4_8WTxAulTr9a7XgMTgi_OKA_Iad1-hMzdD2z0snaq3BE&fref=nf&__xts__%5B0%5D=68.ARAUlT1DqV2wkn3VWI7belmtRn5k9XXk05WUw1Gl88lm2CDjGSmYX0zI_p8A19EG2w6iWF3t0gZN-GurkpGl05KGdS5Xv8lC__sOmlxD8Jz5CrhOjbz1ThvYtiZA3yzk4P66-V5BjDty&__tn__=kCH-R\n"
+ "https://www.facebook.com/profile.php?id=100010004806283&fref=pb&hc_location=friends_tab";
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