Hello everyone, there is such a search code:

//считываем текст с файла AssetManager am = getAssets(); InputStream is = am.open("file.txt"); ByteArrayOutputStream result = new ByteArrayOutputStream(); byte[] buffer = new byte[1024]; int length; while ((length = is.read(buffer)) != -1) { result.write(buffer, 0, length); } String xxx = result.toString("UTF-8"); is.close(); //ищем и выводим строки по паттерну String reg = String.format("(.*)(%s)(.*)", edittext.getText().toString()); textview.setText(""); String[] mf = xxx.split(System.getProperty("line.separator")); for (String str : mf) if (Pattern.matches(reg, str)) textview.setText(textview.getText()+str+"\n"); 

I can’t understand why, stupidly, when reading text from a file, it does not look for regularity matches in it, but if you create, say, String xxx = new String("A\nB\nC"); then everything works fine. I tried to change the encoding, methods of reading from the file, but still it normally creates an array, but does not find matches (or only in the last line).

Do not tell me what is wrong?

  • @zRrr no, just to understand aaa corrected on the textview, and then forgot, correct, thanks - Flappy
  • xxx read the contents of a file? - Vladyslav Matviienko

1 answer 1

Try this:

 String reg = String.format("(.*)(%s)(.*)", edittext.getText().toString()); Pattern pattern = Pattern.compile(reg); StringBuilder sb = new StringBuilder(); try { BufferedReader reader = new BufferedReader(new InputStreamReader(getAssets().open("file.txt"))); String line; while ((line = reader.readLine()) != null) { if (pattern.matcher(line).matches()) { sb.append(line).append("\n"); } } } catch (IOException e) { e.printStackTrace(); } textview.setText(sb.toString()); 
  • Thank you very much, it works, but now it searches only if 1 character is in the edittext, if more is not. Don't know what the problem is now? - Flappy
  • @Flappy is hard to say, I checked it myself, it is searched for the occurrence of a substring correctly. It is necessary debazhit. - Yura Ivanov