Good day! Task: "From the file array, select files with a specific text and date in the file name (there is more text between them)". I am trying to solve the problem using regular expressions.

List fileNames=Arrays.stream(files).filter(p>Pattern.compile("ะ ะตะตัั‚ั€ ัะดะตะปะพะบ("+date.toString("dd.MM.yyyy")+")").matches(p.getName()).collect; 
  • And what does not work? - Jagailo
  • it is impossible to write the filter condition syntactically correctly using the regular list according to 2 criteria: "Register of transactions" and "date". Newbie :) I ask for help - russo tyrusto

2 answers 2

In general, a regular expression for a file name that contains the words "registry of transactions" and the specific date will be as follows:

 ^.*ั€ะตะตัั‚ั€\s+ัะดะตะปะพะบ.*{ะดะฐั‚ะฐ}.*$ 
  • ^ - beginning of line
  • .* - any number of any characters
  • \s+ - one or more spaces
  • {ะดะฐั‚ะฐ} - the date to be found (in plain text, escaping dots \. , because a dot in a regular expression means any character )
  • $ - end of line

It is better to first prepare a regular expression pattern, and then use it. For example, in your case, you can prepare it as follows:

 Date date = new Date(); SimpleDateFormat format = new SimpleDateFormat("dd\\.MM\\.yyyy"); Pattern pattern = Pattern.compile( String.format("^.*ั€ะตะตัั‚ั€\\s+ัะดะตะปะพะบ.*%s.*$", format.format(date)), Pattern.CASE_INSENSITIVE | Pattern.UNICODE_CASE ); 
  • date - in this case, the current date, but you can use any other
  • Date formatting with the dd\\.MM\\.yyyy template for screening points
  • use of CASE_INSENSITIVE flags CASE_INSENSITIVE that the case of letters and UNICODE_CASE not taken into account, so that the case of letters for the Russian alphabet is correctly processed

Then you can use this template in the filtering function:

 List<String> list = Arrays.stream(files) .filter(f -> pattern.matcher(f).matches()) .collect(Collectors.toList()); 
  • thank you very much! - russo tyrusto

Here is an example:

 import java.util.regex.Matcher; import java.util.regex.Pattern; final String sometext = "ะ ะตะตัั‚ั€ ัะดะตะปะพะบ"; final String regex = "(" + sometext + ".*)\\((\\d{2}\\.\\d{2}\\.\\d{4})\\)"; final String string = "ะ ะตะตัั‚ั€ ัะดะตะปะพะบ(03.08.2017)"; final Pattern pattern = Pattern.compile(regex); final Matcher matcher = pattern.matcher(string); while (matcher.find()) { System.out.println("Full match: " + matcher.group(0)); for (int i = 1; i <= matcher.groupCount(); i++) { System.out.println("Group " + i + ": " + matcher.group(i)); } }