【发布时间】:2023-03-22 17:35:01
【问题描述】:
public static void main(String[] args) {
String str = "astv*12atthh124ggh*dhr1234sfff123*dgdfg1234*mnaoj";
Pattern p = Pattern.compile("\\d+");
Matcher m = p.matcher(str);
List<String> strings = new ArrayList<String>();
List<Integer> nums = new ArrayList<Integer>();
while (m.find()) {
nums.add(Integer.parseInt(m.group()));
}
p = Pattern.compile("[a-z]+");
m = p.matcher(str);
while (m.find()) {
strings.add(m.group());
}
System.out.println(nums);
System.out.println(strings);
}
输出:
[12, 124, 1234, 123, 1234]
[astv, atthh, ggh, dhr, sfff, dgdfg, mnaoj]
但我想要这样的输出:
[12124, 1234123, 1234]
[astv, atthhggh, dhrsfff, dgdfg, mnaoj]
【问题讨论】:
-
您为什么不想要
12、124和123?您需要提供一些选择标准。 -
请澄清您的问题。你能解释一下你试图实现的逻辑吗?
-
我有一个类似 "astv*12atthh124ggh*dhr1234sfff123*dgdfg1234*mnaoj" 的字符串,我想将数字和字符串分别拆分成这样的列表 [astv, atthhggh, dhrsfff, dgdfg, mnaoj] [12124, 1234123, 1234]
-
您需要澄清为什么
12124在列表中而12不在列表中。两者都出现在字符串中,但只有前者在您的列表中。 -
@ManojKumarGovindharaj 是的,这很清楚。我不明白为什么
12和124应该组合在一起,而124和1234不应该。
标签: java arrays string list replace