How to find if Java string contains X or Y and contains Z

I'm sure regex is the way to go, but I get a headache when I try to work out a specific regex.

What regex do I need to find if a Java string (contains text "ERROR" or text "WARNING") AND (contains text "parsing") where all matches are case insensitive?

EDIT: I presented a specific case, but my problem is more general. There may be other sentences, but they are all associated with a specific word, ignoring the case. There may be sentences of 1, 2, 3 or more.

+2


source to share


8 answers


If you're not 100% comfortable with regular expressions, don't try to use them for something like this. Just do this instead:

string s = test_string.toLowerCase();
if (s.contains("parsing") && (s.contains("error") || s.contains("warning")) {
    ....

      

because when you come back to your code after six months, you will immediately understand it.



Edit: Here's a regex used:

(?i)(?=.*parsing)(.*(error|warning).*)

      

but it is rather ineffective. For cases where you have an OR condition, a hybrid approach where you search for a few simple regexes and programmatically concatenate the results from Java is usually better, both from a readability and efficiency standpoint.

+17


source


If you really want to use regular expressions, you can use the positive lookahead operator :

(?i)(?=.*?(?:ERROR|WARNING))(?=.*?parsing).*

      



Examples:

Pattern p = Pattern.compile("(?=.*?(?:ERROR|WARNING))(?=.*?parsing).*", Pattern.CASE_INSENSITIVE); // you can also use (?i) at the beginning
System.out.println(p.matcher("WARNING at line X doing parsing of Y").matches()); // true
System.out.println(p.matcher("An error at line X doing parsing of Y").matches()); // true
System.out.println(p.matcher("ERROR Hello parsing world").matches()); // true       
System.out.println(p.matcher("A problem at line X doing parsing of Y").matches()); // false

      

+6


source


try:

 If((str.indexOf("WARNING") > -1 || str.indexOf("ERROR") > -1) && str.indexOf("parsin") > -1)

      

0


source


Regular expressions are not needed here. Try the following:

if((string1.toUpperCase().indexOf("ERROR",0) >= 0 ||  
  string1.toUpperCase().indexOf("WARNING",0) >= 0 ) &&
  string1.toUpperCase().indexOf("PARSING",0) >= 0 )

      

It also takes into account the case-insensitive criteria.

0


source


I usually use this applet to experiment with reg. ex. The expression might look like this:

if (str.matches("(?i)^.*?(WARNING|ERROR).*?parsing.*$")) {
...

      

But as said above, it's best not to use reg. ex. here.

0


source


I think this regex will do the trick (but there must be a better way to do it):

(.*(ERROR|WARNING).*parsing)|(.*parsing.*(ERROR|WARNING))

      

0


source


If you have a variable number of words that you want to match, I would do something like this:

String mystring = "Text I want to match";
String[] matchings = {"warning", "error", "parse", ....}
int matches = 0;
for (int i = 0; i < matchings.length(); i++) {
  if (mystring.contains(matchings[i]) {
    matches++;
  }
}

if (matches == matchings.length) {
   System.out.println("All Matches found");
} else {
   System.out.println("Some word is not matching :(");
}

      

Note. I did not compile this code, so it may contain typos.

0


source


If multiple expressions are used, the .*

parser will throw thousands of "disabled and repeated" test matches.

Never use .*

at the beginning or in the middle of a RegEx pattern.

0


source







All Articles