import java.util.regex.Matcher; import java.util.regex.Pattern; public class StringProcessor { public static void main(String[] args) { String text = "asd oOsdwe ertyu ghjLK Ewesldk"; String substrings[] = text.split(" "); for (String word : substrings) { String regex = "[AaOrK]"; Pattern p = Pattern.compile(regex); Matcher m = p.matcher(word); boolean res = m.find(); System.out.print(" returns: " + " " + res); } } } 

This code checks the text for the presence of letters [AaOrK]. I need to display only incorrect values.

  • This code checks the text for the presence of letters [AaOrK]. I need the wrong values ​​to be output only - Dima Stan

2 answers 2

In this case, print only the words for which the find method returns false :

 String text = "asd oOsdwe ertyu ghjLK Ewesldk"; String substrings[] = text.split(" "); String regex = "[AaOrK]"; Pattern p = Pattern.compile(regex); for (String word : substrings) { Matcher m = p.matcher(word); if (!m.find()) { System.out.print("Not found for " + word); } } 

Creating a Pattern -a makes sense to take out for the cycle: there is no need to create it again each time.

  • Yes, thank you very much) - Dima Stan
  • @ DimaStan on health. If you are satisfied with the answer, do not forget to mark it as appropriate (checkmark to the left of the answer). - Regent

And what exactly is the problem?

 boolean res = m.find(); if (!res) { // res == false System.out.print(" returns: " + word); } 

An example on ideone .

  • and so that it displays just a line without false - Dima Stan
  • @ DimaStan you know java ? updated answer. Do not forget to mark one of the answers as the correct check mark opposite. - Denis