美好的一天,我有Strings

 "bus-nr-7b-Station-Yellow-Street",
 "bus-nr-8-Station-Lewis-Street",
 "train-nr-9-Station-Yield-Street",
 "bus-nr-10-Station-Harrow",
 "train-nr-10a-Station-Booki",
 "train-nr-11-Station-Horope",
 "bus-nr-12-Station-Erstin",
 "bus-nr-13-Station-Green-Street",
 "train-nr-13ab-Station-Final-Station",


最后,我想:

 "B:7b",
 "B:8",
 "T:9",
 "B:10",
 "T:10a",
 "T:11",
 "B:12",
 "B:13",
 "T:13ab",


因此,我想用-B:替换第二个T:之前的所有内容,并删除第三个-之后的所有内容。我如何使用正则表达式来实现呢?

最佳答案

在您的情况下,不使用正则表达式会更快:

String foo = "bus-nr-7b-Station-Yellow-Street";
String[] parts = foo.split("-");
String result = Character.toUpperCase(foo.charAt(0)) + ":" + parts[2];

关于java - 正则表达式:删除第二个“-”之前和之后的所有内容,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/34745450/

10-09 21:04