They are fundamentally different.
(ex1)|(ex2)|(ex3) defines a sequence of alternating capture groups for the literal text ex1 , ex2 and ex3 . That is, either ex1 , if present, will be fixed in the first capture group; or ex2 , if any, will be recorded in the second capture group; or ex3 , if any, will be recorded in the third group. (This would be a rather odd expression, more likely would be (ex1|ex2|ex3) , which matches and captures either ex1 , ex2 , or ex3 .)
[(ex1)(ex2)(ex3)] defines a character class that will match any of the following characters (just one character): (ex1)23 . There are no capture groups, the text in [] processed literally.
The documentation for the Pattern class details how patterns work.
source share