import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Example {
public static void main(String[] args) {
final String regex = "(?x) # free-spacing mode\n"
+ "(?(DEFINE) \n"
+ " # decimal to decimal: 1.2% to 21.53%\n"
+ " (?<dec_to_dec>\n"
+ " (?:[0-9]{1,3}[\\,\\.][0-9]{1,3}[\\%]\\sto\\s[0-9]{1,3}[\\,\\.][0-9]{1,3}[\\%])\n"
+ " ) \n"
+ " # Decimal to Whole number: 1.2% to 21%\n"
+ " (?<dec_to_int>\n"
+ " (?:[0-9]{1,3}[\\,\\.][0-9]{1,3}[\\%]\\sto\\s[0-9]{1,3}[\\%])\n"
+ " )\n"
+ " # Whole number to Decimal: 1% to 21.53%\n"
+ " (?<int_to_dec>\n"
+ " (?:[0-9]{1,3}[\\%]\\sto\\s[0-9]{1,3}[\\,\\.][0-9]{1,3}[\\%])\n"
+ " )\n"
+ " # Whole number to whole number: 1% to 21%\n"
+ " (?<int_to_int>\n"
+ " (?:[0-9]{1,3}[\\%]\\sto\\s[0-9]{1,3}[\\%])\n"
+ " ) \n"
+ " # Single Whole Number: 21%\n"
+ " (?<single_int>\n"
+ " (?:[0-9]{1,3}[\\%])\n"
+ " )\n"
+ " # Single Whole Number: 21%\n"
+ " (?<single_dec>\n"
+ " (?:[0-9]{1,3}[\\,\\.][0-9]{1,3}[\\%])\n"
+ " )\n"
+ ")\n"
+ "(?&dec_to_dec)|(?&dec_to_int)|(?&int_to_dec)|(?&int_to_int)|(?&single_int)|(?&single_dec)";
final String string = "France: 2.8% to 21%";
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