Check if a string has a particular format?

format, java, regex, string

Solution

You can try using this regex:

String x = "9.8.7";
boolean matches = x.matches("\\d\\.\\d\\.\\d"); // true

Notice that the dot `.` is being escaped `\\.`, because it has a special meaning in regex.

Here some input/output samples:

"99.8.7"   -> false
"9.9.7."   -> false
"9.97"     -> false

Problem

I need a `boolean` to check if a given string follows the below format: x.x.x or 1.2.3 , where x is a single digit ``` if string format == x.x.x then TRUE. if string format != x.x.x then FALSE. ``` How can I achieve this?

Original source