
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Capturing Groups and Back References in Java Regex
Capturing groups are a way to treat multiple characters as a single unit. They are created by placing the characters to be grouped inside a set of parentheses. For example, the regular expression (dog) creates a single group containing the letters "d", "o", and "g".
Capturing groups are numbered by counting their opening parentheses from the left to the right.
In the expression ((A)(B(C))), for example, there are four such groups -
- ((A)(B(C)))
- (A)
- (B(C))
- (C)
Back references allow repeating a capturing group using a number like \# where # is the group
number. See the example below −
Example
import java.util.regex.Matcher; import java.util.regex.Pattern; public class Tester { public static void main(String[] args) { //2 followed by 2 five times String test = "222222"; String pattern = "(\d\d\d\d\d)"; Pattern r = Pattern.compile(pattern); Matcher m = r.matcher(test); if (m.find( )) { System.out.println("Matched!"); }else { System.out.println("not matched!"); } //\1 as back reference to capturing group (\d) pattern = "(\d)\1{5}"; r = Pattern.compile(pattern); m = r.matcher(test); if (m.find( )) { System.out.println("Matched!"); }else { System.out.println("not matched!"); } } }
Output
Matched! Matched!
Advertisements