I have written the following Java code:
public void test(final String myString){
final String rule = "^[A-Z]\\d{2}(\\.\\d){0,2}$";
final Pattern pattern = Pattern.compile(rule);
final Matcher matcher = pattern.matcher(myString);
if(!matcher.matches()){
System.out.println("Failure, the String" + myString + " is not valid!");
}
}
The Regular Expression should by valid the following String:
[character are required][number are required][number are required][point is optional][number is optional][number is optional]
It is important, that if a point was declared in the string, at least one Number must be followed!
My solution only works for Strings like J45 or J45.9
Java Java like these are allowed:
D99
M00.0
M01.6
J98.3
T05.0
M96.81
D68.20
Java Strings like these are not allowed:
9D.0
6G
7H.
M96.811
J234.82
G687.1
GU87.11
How I can solve this problem by using Regular Expressions in Java?