Change the regular expression to abc(.*)def(.*)pqr(.*)xyz , and the brackets will be automatically bound to
See the documentation for the class template , especially Groups and Captures , for more information.
Code example:
final String needle = "abc(.*)def(.*)pqr(.*)xyz"; final String hayStack = "abcXdefYpqrZxyz"; // Use $ variables in String.replaceAll() System.out.println(hayStack.replaceAll(needle, "_$1_$2_$3_")); // Output: _X_Y_Z_ // Use Matcher groups: final Matcher matcher = Pattern.compile(needle).matcher(hayStack); while(matcher.find()){ System.out.println( "A: " + matcher.group(1) + ", B: " + matcher.group(2) + ", C: " + matcher.group(3) ); } // Output: A: X, B: Y, C: Z
Sean Patrick Floyd
source share