如何使用 java 从像 "abc#{xxxx}def#{yyyy}ghi" 这样的字符串中获取像 [ "#{xxxx}" , "#{yyyy}"] 这样的字符串数组?



如何从"abc"这样的字符串中获得["#{xxxx}","#{yyyy}"]这样的字符串数组#{xxxx}def#{yyyy}ghi">使用java?

我英语不好,所以我必须努力表达我的问题。我想去掉uel表达式,所以我认为可能有一些方法可以解决这种情况。

Scanner sc = new Scanner(System.in);
String input = sc.nextLine();
//remove first substring from input
String formattedInput = input.substring(input.indexOf("#"), input.lastIndexOf("}") + 1);
//make a regex that checks for string enclosed in } #{
String regex = "(?<=[}])[A-Za-z]*(?=[#])";
Pattern p = Pattern.compile(regex);
Matcher m = p.matcher(formattedInput);
//remove the characters between } and #{
if (m.find()) {
formattedInput = formattedInput.replaceAll(regex, "");
}

System.out.println(formattedInput);
}

输入:abc#{xxxx}def#{yyyy}

输出:#{xxxx}#{yyyy}

我真的不确定你想问什么,因为你的问题措辞不正确,但这段代码将删除#{}标记中没有包含的任何字符。然后,您可以将结果字符串拆分为一个数组。我希望这能帮助

最新更新