In the Pattern class it says there are two types of regex: capturing and non-capturing, but I don’t understand the difference.
http://docs.oracle.com/javase/6/docs/api/java/util/regex/Pattern.html#special
How are they different? When do I have to use each one? Any examples?
Consider a pattern where you need to check for a variety of things in a single position, e.g a bunch of different two character patterns. Normally you use the
|alternation operator:which requires use of
()brackets as well. But those brackets also act as a capturing group. Maybe you really don’t want to capture those char sequences, just check for their presence, which is where the non-capturing groups come into play: