How to use backReferences in Regular Expression?
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 (abc) creates a single capturing group. The part of input string matching the capturing group is stored in a "backreference" for late recall. So that the first three characters abc of an input string abcdefg creates a "backreference".
Backreferences enable the programmer to refer back to the saved matching strings. A backreference is specified in the regular expression as a backslash (\) followed by a digit indicating the number of the group to be recalled. capturing groups are numbered by scaning the regular expression from left to right and counting the opening round brackets. The first bracket starts backreference number one, the second number two, etc. Non-capturing parentheses, group begining with (?, are not counted. For example (a(?:bc)(de))uvw\2xyz\1, this regex contains two such groups:
The (?bc) is non-capturing parenthese and is not counted. For easy to understand, you can think about the (a(?:bc)(de))uvw\2xyz\1 regular expression is equivalent to the (a(?:bc)(de))uvw(de)xyz(a(?:bc)(de)) regular expression. The (a(?:bc)(de))uvw\2xyz\1 regex will match abcdeuvwdexyzabcde.
Most Recent java Faqs
- How to uncompress a file in the gzip format?
- How to make a gzip file in Java?
- How to use Java String.split method to split a string by dot?
- How to validate URL in Java?
- How to schedule a job in Java?
- How to return the content in the correct encoding from a servlet?
- What is the difference between JDK and JRE?
Most Viewed java Faqs
- How to read input from console (keyboard) in Java?
- How to Retrieve Multiple Result Sets from a Stored Procedure in JDBC?
- How to Use Updatable ResultSet in JDBC?
- What are class variables in Java?
- What are local variables in Java?
- How to Use JDBC Java to Create Table?
- Why final variable in Enhanced for Loop does not act final?