You could do this.
final String regex = ".*LIST.*\\((?:\"car1\" \"car0\"|\"car0\" \"car1\")\\) RETURN.*\\R";
And if you don't understand what it means, you should familiarize yourself with the documentation for java.util.regex.Pattern
. That link is basically your bible when it comes to writing Regular Expressions in Java.
And if that is not clear enough for you, here is an exploded view of the exact same string, but with a breakdown of what each individual component means.
final String regex = ""
+ ".*" //wildcard -- anything or nothing can go here.
+ "LIST" //the literal string LIST in all-caps
+ ".*" //another wildcard
+ "\\(" //an escaped opening parentheses
+ "(?:" //the opener of a non-capturing capture group
+ "\"" //an escaped double quote
+ "car1" //the literal string car1
+ "\"" //another escaped double quote
+ " " //a single whitespace -- pressing the space bar once
+ "\"" //yet another escaped double quote
+ "car0" //the literal string car0
+ "\"" //4th double quote thus far
+ "|" //This is a very special symbol -- when you place this symbol
//inside of any type of capture group, you should treat it
//like an or operator in Java if statements
+ "\"" //5th double quote thus far
+ "car0" //the literal string car0
+ "\"" //6th double quote thus far
+ " " //another whitespace
+ "\"" //7th double quote thus far
+ "car1" //the literal string car1
+ "\"" //8th double quote thus far -- it is also the last one
+ ")" //the closer of the non-capturing capture group started above
+ "\\)" //an escaped closing parentheses
+ " " //yet another whitespace
+ "RETURN" //the literal string RETURN in all-caps
+ ".*" //yet another wildcard
+ "\\R" //This is a linebreak matcher -- it matches all new line symbols
;
Some things to note.
- Escaping means that you decide to stop interpreting a symbol for its special meaning, and you just want Java to put it into the String. To escape, you use the backslash symbol (\). Escaping can get tricky though, as you can see. Sometimes you need 2 backslashes, and sometimes you need 1. If you need help understanding when and where you need 1 or 2 (or worse yet, more), I would take a look at this link.
Java Regular Expression - how to use backslash
A capture group plus a | symbol allows you to do OR clauses in your regex. The regex above basically says, "match a wildcard, followed by LIST, followed by another wildcard, followed by an opening parentheses, followed by an OR CLAUSE, where one of the following cases must be true. Either we match the literal string "car1" "car0" or the other literal string "car0" "car1". After the OR CLAUSE, we match a closing parentheses, a single whitespace, the literal string RETURN, another wildcard, and then finally, a new line matcher". Which leads to my next point.
With the exception of the OR CLAUSE, everything here is in order. Meaning, one must be matched before the next one can be matched. The OR CLAUSE gives you the ability to branch between one of the options, but that's it. Otherwise, everything follows the rule of going in order.