Regular expression matches classes

I have an array

String[] grades = new String[]{"D","C-","C","C+","B-","B","B+","A-","A","A+"}; 

I want to check if a string is one of these values.

I could loop through an array to execute this, but I want to do this through a regex.

+4
source share
1 answer

The regex is pretty simple:

 [AC][+-]?|D 

The first part says that A through C can be followed by an additional plus or minus; the second part allows D himself.

I could go through the array to execute this

You can also use contains() to do this without a loop:

 if (Arrays.asList(grades).contains(grade)) { ... } 
+12
source

All Articles