Записывает не все слова, k9 игнорирует к примеру, но если проверять его Check Regex - подходит
Что не так ?
Текст для проверки - "1D regular expression, regex or regexp1
(sometimes called a rational expressi4on is a sequence of
characters that define a search pattern. Usually such patterns
are used by string searching algorithms for "find" or "find and replace" operations on strings,
or for input validation. It is a technique developed in theoretical computer science and formal language theory.
The 1concept arose in the 1950s when the American mathematician Stephen Cole Kleene formalized the
description of a regular language. The concept came into common use with Unix text-processing utilities.
Since the 1980s different syntaxes for writing regular expressions exist, one being the POSIX stan98dard
and another, widely used, being the Perl syntax.
k9
0ll9
12345432
Regular expressions are used in search engines, search and replace dialogs
of word processors and text editors, in text processing utilities such as sed and AWK and in lexical analysis.
Many programming languages provide regex capabilities either built-in or via libraries."
package com.javarush.task.task19.task1923;
/*
Слова с цифрами
*/
import com.sun.org.apache.xerces.internal.impl.xpath.regex.Match;
import java.io.*;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Solution {
static List<String> stringsWithNumbers = new ArrayList<>();
public static void main(String[] args) throws IOException {
String firstFileName = args[0];
String secondFileName = args[1];
String[] fileData = getFileData(firstFileName).toString().split(" ");
findWordsWithNumbers(fileData);
String newData = createNewFileData(stringsWithNumbers).toString();
writeToFile(secondFileName, newData);
}
public static StringBuilder getFileData(String fileName) throws IOException {
BufferedReader fileReader = new BufferedReader(new FileReader(fileName));
StringBuilder fileWords = new StringBuilder();
while (fileReader.ready()) {
fileWords.append(fileReader.readLine() + "\n");
}
fileReader.close();
return fileWords;
}
public static void findWordsWithNumbers(String[] fileData) {
for (String data : fileData) {
if (data.matches(".*\\d+.*")) {
stringsWithNumbers.add(data + " ");
}
}
}
public static void writeToFile(String fileName, String data) throws IOException {
BufferedWriter fileWriter = new BufferedWriter(new FileWriter(fileName));
fileWriter.write(data);
fileWriter.close();
}
public static StringBuilder createNewFileData(List oldFileData) {
StringBuilder toFile = new StringBuilder();
for (int i = 0; i < oldFileData.size(); i++) {
toFile.append(oldFileData.get(i));
}
return toFile;
}
}