GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing docs.oracle.com — Capturing Groups (The Java™ Tutorials > Essential Java Classes > Regular Expressions) Documentation
The Java™ Tutorials
Hide TOC
Capturing Groups
Trail: Essential Java Classes
Lesson: Regular Expressions The Java Tutorials have been written for JDK 8. Examples and practices described in this page don't take advantage of improvements introduced in later releases and might use technology no longer available. Capturing GroupsIn the previous section, we saw how quantifiers attach to one character, character class, or capturing group at a time. But until now, we have not discussed the notion of capturing groups in any detail. 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 NumberingAs described in the
To find out how many groups are present in the expression, call the There is also a special group, group 0, which always represents the entire expression. This group is not included in the total reported by It's important to understand how groups are numbered because some
BackreferencesThe section of the input string matching the capturing group(s) is saved in memory for later recall via backreference. A backreference is specified in the regular expression as a backslash ( To match any 2 digits, followed by the exact same two digits, you would use Enter your regex: (\d\d)\1 Enter input string to search: 1212 I found the text "1212" starting at index 0 and ending at index 4. If you change the last two digits the match will fail: Enter your regex: (\d\d)\1 Enter input string to search: 1234 No match found. For nested capturing groups, backreferencing works in exactly the same way: Specify a backslash followed by the number of the group to be recalled. About Oracle | Contact Us | Legal Notices | Terms of Use | Your Privacy Rights Copyright © 1995, 2024 Oracle and/or its affiliates. All rights reserved.
Previous page: Quantifiers
Next page: Boundary Matchers |
| You are visitor 01402503 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |