如何在Java中拆分特定条件?

时间:2022-08-03 22:18:25

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?

在上面的字符串中,我只想要“04-Dec”部分,这可能会改为“12-Jan”,就像这个或带有该格式的月份的任何日期一样。我该怎么做呢?

1 个解决方案

#1


2  

You can split using / and get the value 2

您可以使用/拆分并获取值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}:

或者您可以使用此正则表达式\ 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());
}

#1


2  

You can split using / and get the value 2

您可以使用/拆分并获取值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}:

或者您可以使用此正则表达式\ 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());
}