Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/311.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在Java中使用特定条件进行拆分?_Java_String - Fatal编程技术网

如何在Java中使用特定条件进行拆分?

如何在Java中使用特定条件进行拆分?,java,string,Java,String,假设我有一根像 "resources/json/04-Dec/someName_SomeTeam.json" 在上面的字符串中,我只需要“04 Dec”部分,它可能会更改为“1月12日”,就像这样,或者任何带有该格式月份的日期。如何执行此操作?您可以使用/进行拆分,并获取值2 String text = "resources/json/04-Dec/someName_SomeTeam.json"; String[] split = text.split("\\/"); String resul

假设我有一根像

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

在上面的字符串中,我只需要“04 Dec”部分,它可能会更改为“1月12日”,就像这样,或者任何带有该格式月份的日期。如何执行此操作?

您可以使用
/
进行拆分,并获取值
2

String text = "resources/json/04-Dec/someName_SomeTeam.json";
String[] split = text.split("\\/");
String result = split[2];//04-Dec
或者,您可以将模式与此正则表达式一起使用:

为什么不检查javadoc呢
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());
}