Search icon CANCEL
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Conferences
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
Java 9 Regular Expressions

You're reading from   Java 9 Regular Expressions A hands-on guide to implement zero-length assertions, back-references, quantifiers, and many more

Arrow left icon
Product type Paperback
Published in Jul 2017
Publisher Packt
ISBN-13 9781787288706
Length 158 pages
Edition 1st Edition
Languages
Arrow right icon
Author (1):
Arrow left icon
Anubhava Srivastava Anubhava Srivastava
Author Profile Icon Anubhava Srivastava
Anubhava Srivastava
Arrow right icon
View More author details
Toc

Table of Contents (9) Chapters Close

Preface 1. Getting Started with Regular Expressions 2. Understanding the Core Constructs of Java Regular Expressions FREE CHAPTER 3. Working with Groups, Capturing, and References 4. Regular Expression Programming Using Java String and Scanner APIs 5. Introduction to Java Regular Expression APIs - Pattern and Matcher Classes 6. Exploring Zero-Width Assertions, Lookarounds, and Atomic Groups 7. Understanding the Union, Intersection, and Subtraction of Character Classes 8. Regular Expression Pitfalls, Optimization, and Performance Improvements

Back references

Back references provide a convenient way of matching a repeated character or repeated tokens in the input text. By using back references, the regular expression engine can match the exact same text as previously matched by a capturing group.

The syntax of a back reference is a backslash followed by a capturing group number, as shown in the following example:

    \3 

The preceding example is a back reference of the third capturing group.

In Java regular expressions, there can be up to 99 back references, each number referencing a captured group number.

For example, if we need to match a two-digit number with the restriction that both digits must be the same, then we need to capture the first digit and then use a back reference for the first captured group, as follows:

    ^(\d)\1$ 

Now, this regex will match any of these strings: 11, 22, 55, and 88.

We can also...

lock icon The rest of the chapter is locked
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at €18.99/month. Cancel anytime