我有像KA01F332-9:25,KA01F212-9:27,KA01F242-9:35,KA01F232-9:45,
这样的长字符串。这些是车辆号和时间的组合。现在我想将KA01F242
的时间替换为10:20
我该怎么做。
到目前为止,我已经做到了这一点。
String busEAT=KA01F332-9:25,KA01F212-9:27,KA01F242-9:35,KA01F232-9:45,;
busEAT.subString(busEAT.indexOf(vno),busEAT.indexOf(','));
但我没有得到确切的价值。它必须动态完成,任何人都可以帮助我。
答案 0 :(得分:2)
您可以使用这样的正则表达式:
String s = "KA01F332-9:25,KA01F212-9:27,KA01F242-9:35,KA01F232-9:45,";
System.out.println(s.replaceFirst("(?<=KA01F242-).*?(?=,)", "10:20"));
// Positive look-behind for "KA01F242" and positive look-ahead for "," . They are just matched but not captured, so they will not get replaced.
O / P:
KA01F332-9:25,KA01F212-9:27,KA01F242-10:20,KA01F232-9:45,
编辑:
"(?<=KA01F242-).*?(?=,)", "10:20")
- &gt;首先查找“KA01F242”(正面后视)之前的任何字符,然后选择所有字符(“KA01F242”刚刚匹配,未选中。)接下来,选择所有连续字符,直到获得逗号(是agin匹配,未选中)