import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Example {
public static void main(String[] args) {
final String regex = "\\[ (?# match [ literally)\n"
+ "( (?# start capture group)\n"
+ " http (?# match http literally)\n"
+ " \\S+ (?# match 1+ non-whitespace characters)\n"
+ ") (?# end capture group)\n"
+ "\\s+ (?# match 1+ whitespace characters)\n"
+ "( (?# start capture group)\n"
+ " [^\\]]+ (?# match 1+ non-] characters)\n"
+ ") (?# end capture group)\n"
+ "\\] (?# match ] literally)\n"
+ "(?: (?# start non-capturing group)\n"
+ " \\s+ (?# match 1+ whitespace characters)\n"
+ " from (?# match from literally)\n"
+ " (?: (?# start non-capturing group)\n"
+ " \\s+ (?# match 1+ whitespace characters)\n"
+ " the (?# match the literally)\n"
+ " )? (?# end optional non-capturing group)\n"
+ " \\s+ (?# match 1+ whitespace characters)\n"
+ " \\[\\[ (?# match [[ literally)\n"
+ " ( (?# start capturing group)\n"
+ " .*? (?# lazily match 0+ characters)\n"
+ " ) (?# end capturing group)\n"
+ " \\]\\] (?# match ]] literally)\n"
+ ")? (?# end optional non-caputring group)";
final String string = "{{Wikibooks|Wikijunior:Countries A-Z|France}} {{Sister project links|France}} * [http://www.bbc.co.uk/news/world-europe-17298730 France] from the [[BBC News]] * [http://ucblibraries.colorado.edu/govpubs/for/france.htm France] at ''UCB Libraries GovPubs'' *{{dmoz|Regional/Europe/France}} * [http://www.britannica.com/EBchecked/topic/215768/France France] ''Encyclopædia Britannica'' entry * [http://europa.eu/about-eu/countries/member-countries/france/index_en.htm France] at the [[European Union|EU]] *{{Wikiatlas|France}} *{{osmrelation-inline|1403916}} * [http://www.ifs.du.edu/ifs/frm_CountryProfile.aspx?Country=FR Key Development Forecasts for France] from [[International Futures]] ;Economy *{{INSEE|National Institute of Statistics and Economic Studies}} * [http://stats.oecd.org/Index.aspx?QueryId=14594 OECD France statistics]";
final Pattern pattern = Pattern.compile(regex, 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