Did you mean :
String text = "1:99:33 - some text";
boolean check = text.matches("\\d+:\\d+:\\d+ - .*");
System.out.println(check);
If you want to match exact (one number):(two numbers):(two number) you can use \\d:\\d{2}:\\d{2}
instead of \\d+:\\d+:\\d+
details
\\d+
match one or more digit
:
literal character
\\d+
match one or more digit
:
literal character
\\d+
match one or more digit
-
one space hyphen one space
.*
zero or more any character
...how do I extract the numbers and the text from the string?
If you are using Java 8 you can split your input, the first input return numbers separated by :
, the second is the text you want, so to extract the numbers, you need to split the first input again by :
then Iterate over them and convert each one to an Integer, like this :
String input = "1:99:33 - some text";
String[] split = input.split(" - ");//Split using space hyphen space
String text = split[1];//this will return "some text"
List<Integer> numbers = Arrays.asList(split[0].split(":")).stream()
.map(stringNumber -> Integer.parseInt(stringNumber))
.collect(Collectors.toList());// this will return list of number [1, 99, 33]