Can you please tell me how to rewrite this search method in Java?

var reg = string.Format(".*({0}).*", texbox.Text); Regex any = new Regex(reg); var mf = a.Split('\n'); textblock.Text = string.Empty; foreach (var f in mf) if (any.IsMatch(f)) textblock.Text += f + Environment.NewLine; 

Here is my option

 String reg = String.format(".*%S.*", Pattern.quote(edittext.getText().toString())); Pattern any = Pattern.compile(reg); String[] mf = str_data.split("\n"); textview.setText(""); for (String f : mf) { if (any.matcher(f).matches()) { textview.append(f + "\n"); } } 

But the result is not displayed. Looks like a mistake in the condition. I have no idea how to write it is equivalent to the code on the Sharpe.

  • and what a mistake in the result? - abbath0767
  • @ abbath0767 does not show matching str_data strings in the text - Flappy
  • It seems to me, or did you forget to move the brackets in the regular season? ".*({0}).*" => ".*(%S).*" Otherwise, everything seems fine - rdorn
  • @rdorn made with brackets, anyway, either the last element shows or nothing - Flappy

1 answer 1

Try so

 String regex = String.format(".*(%s).*", Pattern.quote(edittext.getText().toString())); String[] array = str_data.split("\n"); textview.setText(""); for (String str : array) if (Pattern.matches(regex, str)) textview.append(str + "\n"); 
  • All the same, text shows either nothing, or the last line of str_data, on Sharpe perfectly displays elements in lines that have a pattern match - Flappy
  • very strange, give an example, on which lines it does not work - Artem Konovalov