If you want to match a pattern like:
1y2m3d45h6mi7s
You could use the following regex (online demo here):
(?:(\d+)y)?(?:(\d+)m(?!i))?(?:(\d+)d)?(?:(\d+)h)?(?:(\d+)mi)?(?:(\d+)s)?
As you can see, it consists in several parts like (?:(\d+)X)?, where X is the character for the time period you want to match. It means:
(?: open parenthesis, for "non-matching group"
(\d+) any number of digits
X followed by the character 'X'
)? and everything is optional
Also, for month, (?:(\d+)m(?!i))?, there is a negative lookahead to make it consider 1mi one minute instead of one month plus the char i (from another information, that is not the date).
And some Java code to work with it (online demo here):
public static void main(String[] args) throws java.lang.Exception {
parseInformation("1y30d");
parseInformation("1y2m30mi");
parseInformation("1y1mi");
parseInformation("1y2m3d4h5mi6s");
}
public static void parseInformation(String information) {
Pattern p = Pattern.compile("(?:(\\d+)y)?(?:(\\d+)m(?!i))?(?:(\\d+)d)?(?:(\\d+)h)?(?:(\\d+)mi)?(?:(\\d+)s)?");
Matcher m = p.matcher(information);
while (m.find()) {
if (m.group().isEmpty()) { continue; /* found nothing, go on */ }
System.out.println(information + " found: '"+m.group()+"'");
System.out.println("\t" + m.group(1) + " years");
System.out.println("\t" + m.group(2) + " months");
System.out.println("\t" + m.group(3) + " days");
System.out.println("\t" + m.group(4) + " hours");
System.out.println("\t" + m.group(5) + " minutes");
System.out.println("\t" + m.group(6) + " seconds");
System.out.println("");
}
Output:
...
1y2m3d4h5mi6s found: '1y2m3d4h5mi6s'
1 years
2 months
3 days
4 hours
5 minutes
6 seconds