Nisha Shlaymoon Nisha Shlaymoon - 4 months ago 12
Java Question

Java regex split String into an array of numbers

I have a scanner String input in the following format: 12:00:00PM.

I managed to finally isolate PM or AM by this:

public class ConverTime
public static void main(String[] args)
Scanner sc = new Scanner(;
String timeString =;

String[] pmOrAm = timeString.split("\\d");
String[] nums = timeString.split("(: + \\D)");

// PM or AM
String timeType = pmOrAm[pmOrAm.length - 1];
for (String n: nums)

I know the way I isolate PM or AM is not the best, so hopefully there is a better way where i can get an array of exactly one string.

But when I run the for loop for the "nums" array, the output is:


I want the output to be like so:


without the "PM" or "AM." How do I go about this?


an easy way could be:


Hope it helps