import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Example {
public static void main(String[] args) {
final String regex = "/^[0-9]*(\\.[0-9]{0,2})?$/";
final String string = "1.22\n"
+ "3\n"
+ "999\n"
+ "-3\n"
+ "0\n"
+ "0.0\n"
+ "1.0\n"
+ "0.1\n"
+ ".01\n"
+ "-0.0000000000000000000000000000000001\n"
+ "2e3\n"
+ "-2e3\n"
+ "2e-3\n"
+ "-2e-3\n"
+ "1.0e0\n"
+ "0.1e1\n"
+ ".01e-1\n"
+ "-9.1e3\n"
+ "-9.1e-3\n"
+ "9E5\n"
+ "0e-11\n"
+ "0xff\n"
+ "0XFF\n"
+ "0x0\n"
+ "0xfe3\n"
+ "-0\n"
+ "-0.0\n"
+ "-0.0000000000000000000000000000000000\n"
+ "-.0\n"
+ "-.0000000000000000000000000000000000\n"
+ "1xff\n"
+ "0x\n"
+ "0x0fg3\n"
+ "0xf4.\n"
+ ".0xf\n"
+ "0xfe-3\n"
+ "-0xff\n"
+ "0.0xff\n"
+ "0xff.1\n"
+ "e89\n"
+ ".e3\n"
+ "001\n"
+ "-00.2\n"
+ "3.\n\n"
+ "a\n"
+ " -1\n"
+ "--1\n"
+ "-.1\n"
+ "2..3\n"
+ "2-\n"
+ "2...3\n"
+ "2.4.3\n"
+ "5-6-7\n"
+ ".-1";
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