Torikul Alam
Torikul Alam

Reputation: 549

How to Split with particular condition in Java?

Suppose I have a string like

"resources/json/04-Dec/someName_SomeTeam.json"

In above string I want just "04-Dec" part, this may change to "12-Jan" like this or any date with month with that format. How do I do this?

Upvotes: 1

Views: 45

Answers (1)

Youcef LAIDANI
Youcef LAIDANI

Reputation: 59978

You can split using / and get the value 2

String text = "resources/json/04-Dec/someName_SomeTeam.json";
String[] split = text.split("\\/");
String result = split[2];//04-Dec

Or you can use patterns with this regex \d{2}\-\[A-Z\]\[a-z\]{2}:

String text = "resources/json/04-Dec/someName_SomeTeam.json";
String regex = "\\d{2}\\-[A-Z][a-z]{2}";
Pattern pattern = Pattern.compile(regex);
Matcher matcher = pattern.matcher(text);
if (matcher.find()) {
    System.out.println(matcher.group());
}

Upvotes: 2

Related Questions