题解 | #在字符串中找出连续最长的数字串#正则
在字符串中找出连续最长的数字串
https://www.nowcoder.com/practice/2c81f88ecd5a4cc395b5308a99afbbec
import java.util.*; import java.util.stream.*; import java.util.regex.*; import java.util.function.*; // 注意类名必须为 Main, 不要有任何 package xxx 信息 public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); while (in.hasNextLine()) { List<String> list = numStr(in.nextLine()); System.out.printf("%s,%d\n", String.join("", list), list.get(0).length()); } } static List<String> numStr(String line) { Matcher matcher = Pattern.compile("(\\d+)").matcher(line); LinkedHashMap<String, Integer> map = new LinkedHashMap<>(); int max = Integer.MIN_VALUE; while (matcher.find()) { String aimed = matcher.group(); map.put(aimed, aimed.length()); max = Math.max(max, aimed.length()); } int finalMax = max; List<String> list = map.entrySet().stream().filter(e -> { return e.getValue() == finalMax; }).map(Map.Entry::getKey).collect(Collectors.toList()); return list; } }