0

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?

  • I think you did not provide all testing cases, or your *[point is optional][number is optional][number is optional]* rule is not correct, hence, see two options in my answer. – Wiktor Stribiżew Aug 04 '17 at 06:44

2 Answers2

0

[point is optional][number is optional][number is optional]

You need to make the dot optional and set the {0,2} quantifier to the \d pattern only:

^[A-Z]\d{2}\.?\d{0,2}$

See the regex demo

Details:

  • ^ - start of string anchor
  • [A-Z] - an uppercase ASCII letter
  • \d{2} - any 2 digits
  • \.? - an optional dot
  • \d{0,2} - any 0 to 2 digits
  • $ - end of string.

Since you are using .matches() that anchors the pattern by default, you may declare it without the ^ and $ anchors as

final String rule = "[A-Z]\\d{2}\\.?\\d{0,2}";

See an online Java test.

Or, if there must be 1 or 2 digits after a dot, or if no dot is present 0 to 2 digits are allowed, you may consider using

^[A-Z]\d{2}(?:\.\d{1,2}|\d{0,2})$

See this regex demo, and use as

final String rule = "[A-Z]\\d{2}(?:\\.\\d{1,2}|\\d{0,2})";

where (?:\\.\\d{1,2}|\\d{0,2}) matches either a . and then any 1 or 2 digits, OR any 0 to 2 digits.

Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563
  • Could you explain, why you have defined the character ':' in your last expression? Thenaks ! –  Aug 04 '17 at 07:05
  • @Marwief See [What is a non-capturing group? What does a question mark followed by a colon (?:) mean?](https://stackoverflow.com/questions/3512471). Just grouped two alternatives without capturing the submatches, i.e. without storing those texts in memory. – Wiktor Stribiżew Aug 04 '17 at 07:11
  • @Marwief Look, Anthony interpreted your `[point is optional][number is optional][number is optional]` as `optional( [point is required][number is required][number is required] )`. What are your real requirements? – Wiktor Stribiżew Aug 04 '17 at 08:02
0

This regex expression:

  • Requires one Uppercase Letter followed by 2 number digits
  • Followed by an optional combination of a point and 1-2 number digits

    ^[A-Z]\d{2}(?:\.\d{1,2})?$
    
Anthony
  • 306
  • 2
  • 8