0

Eсть txt фаил (в котором хранятся версии установленных обновлений ПО) мне нужно как то получить самую последнюю версию вот пример ---->

30.11.2020 Обновление 2.218.2.218
27.11.2020 Обновление 2.217.2.217
20.11.2020 Обновление 2.216.2.216

насколько я понял регулярка должна выглядеть вот так (\d{1}\.\d{3}\.\d{1}\.\d{3}) а вот как пройтись по всему документу непонятно

 List<String> lines = Files.readAllLines(Paths.get("\"D:\\d\\Readme.txt\""));

Pattern pattern = Pattern.compile("\\d{1}\\.\\d{3}\\.\\d{1}\\.\\d{3}");

for (String read : lines) {
    
                }
1
  • Наверное, "(?s)(?<!\\d|\\d\\.)(?:\\d\\.\\d{3}){2}(?!\\.?\\d)(?!.*(?<!\\d|\\d\\.)(?:\\d\\.\\d{3}){2}(?!\\.?\\d))" 4 дек 2020 в 12:57

1 ответ 1

0

Я вот так решил

data.txt

30.11.2020 Обновление 2.218.2.218
27.11.2020 Обновление 2.217.2.217
20.11.2020 Обновление 2.216.2.216
public static void main(String[] args) throws Exception {
    File file = new File("C:/data.txt");

    try (BufferedReader br = new BufferedReader(new FileReader(file))) {
      String line;
      String fullText = "";
      while ((line = br.readLine()) != null) {
        fullText += line;
      }
      final String sourceText = fullText;
      Matcher matcher = Pattern.compile("(\\d{1}\\.\\d{3}\\.\\d{1}\\.\\d{3})").matcher(sourceText);
      List<String> matchedResults =
          matcher
              .results()
              .map(result -> sourceText.substring(result.start(), result.end()))
              .collect(Collectors.toList());
      System.out.println(matchedResults);
    }
  }

выхлоп такой

[2.218.2.218, 2.217.2.217, 2.216.2.216]

ну или под ваш вариант

public static void main(String[] args) throws Exception {

    List<String> lines = Files.readAllLines(Paths.get("C:\\data.txt"));

    final String sourceText = lines.stream().collect(Collectors.joining());
    Matcher matcher = Pattern.compile("(\\d{1}\\.\\d{3}\\.\\d{1}\\.\\d{3})").matcher(sourceText);
    List<String> matchedResults =
        matcher
            .results()
            .map(result -> sourceText.substring(result.start(), result.end()))
            .collect(Collectors.toList());
    System.out.println(matchedResults);
  }
2
  • Спасибо огромное) если вас не затруднит оставьте пожалуйста пару комментариев чтобы понимать как оно работает)) 4 дек 2020 в 13:41
  • Тут в основном Stream Api. остальное так себе 4 дек 2020 в 13:44

Ваш ответ

By clicking “Отправить ответ”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Всё ещё ищете ответ? Посмотрите другие вопросы с метками или задайте свой вопрос.