I think your question is phrased ambigously... Are you looking to check if the input strings contains a number within the range 51-55? (Your written text seems to imply this) In this case, the string "512 is a power of two" would not be a match, since it does not contain the number 51.
Or are you looking to check that the string contains the character sequence 51, 52, 53, 54 or 55? (Your code seems to imply this) In this case, the string "512 is a power of two" would be a match, since it contains the character sequence 51.
If the first option is the case, then you'd need something like:
Pattern p = Pattern.compile("(^|[^\\d])5[1-5]($|[^\\d])");
if (p.matcher(inputString).find()) {
//The inputString contains the number you seek
}
Essentially, what this is doing is looking for
- a character sequence 51-55
- preceeded by either the beginning of the string, or by a non-digit character
- followed by either the end of the string, or by a non digit character