It is necessary to parse the date of the form 12:00 = 1200 I am writing a code:

 public class RegexTest { public static void main(String args[]) { String pattern = "[0-9]+"; String text = "12:00"; Pattern p = Pattern.compile(pattern); Matcher m = p.matcher(text); String a = null; while (m.find()) { a = text.substring(m.start(), m.end()) + "*"; } System.out.print(a); } } 

Displays only 00 , why does not write 1200 ?

    1 answer 1

    Isn't it easier to use replace() ?

     String text = "12:00"; String a = text.replace(":", ""); 

    But does not find, because m.find() at the first iteration returns to you "12" , you assign a = "12" . On the second iteration, "00" returned, and you overwrite a = "00" .

    • Thank you) I somehow did not think) - Serjey123