Split a string with a list of keyword matches?

Viewed 81

I have a list of delimiters such as [month, months, years, yrs, years, hrs, hours, mins, min, minute] etc.

And there are input strings such as "2 years 2months", "twenty-two weeks two days" and I would like to break these strings into "2 years" and "2 months" etc. How do it with Java regex?

I tried to approach in the following way and not sure how to proceed from here:

String s = "2 mnths 2 years";
String regex = String.join("|", timeWords);
String[] x = s.split(regex);
1 Answers
Related