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

Java 9 Regular Expressions: A hands-on guide to implement zero-length assertions, back-references, quantifiers, and many more

Arrow left icon
Profile Icon Anubhava Srivastava
Arrow right icon
$19.99 per month
Full star icon Full star icon Full star icon Full star icon Full star icon 5 (2 Ratings)
Paperback Jul 2017 158 pages 1st Edition
eBook
$20.98 $29.99
Paperback
$38.99
Subscription
Free Trial
Renews at $19.99p/m
Arrow left icon
Profile Icon Anubhava Srivastava
Arrow right icon
$19.99 per month
Full star icon Full star icon Full star icon Full star icon Full star icon 5 (2 Ratings)
Paperback Jul 2017 158 pages 1st Edition
eBook
$20.98 $29.99
Paperback
$38.99
Subscription
Free Trial
Renews at $19.99p/m
eBook
$20.98 $29.99
Paperback
$38.99
Subscription
Free Trial
Renews at $19.99p/m

What do you get with a Packt Subscription?

Free for first 7 days. $19.99 p/m after that. Cancel any time!
Product feature icon Unlimited ad-free access to the largest independent learning library in tech. Access this title and thousands more!
Product feature icon 50+ new titles added per month, including many first-to-market concepts and exclusive early access to books as they are being written.
Product feature icon Innovative learning tools, including AI book assistants, code context explainers, and text-to-speech.
Product feature icon Thousands of reference materials covering every tech concept you need to stay up to date.
Subscribe now
View plans & pricing
Table of content icon View table of contents Preview book icon Preview Book

Java 9 Regular Expressions

Understanding the Core Constructs of Java Regular Expressions

Using Java as a regular expression flavor, in this chapter, we will go a bit deeper and learn in detail about anchors, quantifiers, boundary matchers, all the available character classes, negated character classes, predefined character classes, and character classes escaping rules, using Java as a reference point. You will also learn Unicode text matching, using regular expressions in Java. We will also cover greedy versus non-greedy (lazy matching) and the change of regular expression behavior with lazy matching.

We will cover the following topics in this chapter:

  • Anchors and quantifiers
  • Boundary matchers
  • Character classes
  • Regex escaping rules
  • Escaping inside character classes
  • Negated character classes
  • Predefined character classes
  • Unicode characters matching
  • Greedy quantifiers
  • Lazy quantifiers
  • Possessive quantifiers...

Understanding the core constructs of regular expressions

Certain special character constructs are allowed literally in Java regular expressions. Here they are:

Special Character Meaning
\0c A character with the octal value c
\0cc A character with the octal value cc
\0ncc A character with the octal value ncc, where n cannot be more than 3
\xhh A character with the hexadecimal value 0xhh
\uhhhh A character with the hexadecimal value 0xhhhh
\x{h...h} A character with the hexadecimal value 0xh...h, where h must be a valid CODE_POINT
\n Newline character or u000A
\t Tab character or u0009
\r Carriage return character or u000D
\f Form feed character or u000C
\e Escape character or u\u001B
\a Bell character or \u0007
\cn A control character represented by n

Quantifiers

We briefly looked at quantifiers in the first chapter. Quantifiers allow us to quantify the occurrences of our matches. We can match the input in various ways, such as an optional match, an open-ended range, a closed range, and by using a fixed number. Let's take a closer look at them, as quantifiers are integral to most of the regular expressions.

Basic quantifiers

The following table lists all the quantifiers available in Java regular expressions:

...
Quantifier Meaning
m* Match m zero or more times
m+ Match m one or more times
m? Match m one or zero times (also called an optional match)
m{X} Match m exactly X times
m{X,} Match m X or more times
m{X,Y} Match m at least X and at most Y times

Summary

In this chapter, we covered the core constructs of regular expressions in depth. You learned anchors, quantifiers, boundary matchers, various character classes, negated character classes, and predefined character classes. We found out that escaped characters need to be escaped twice in the Java language. You also learned Unicode text matching using regular expressions in Java. We covered greedy, non-greedy, and possessive quantifiers. You learned embedded mode modifiers in Java regular expressions and how they can change the interpretation of a regular expression.

In the next chapter, we will cover another very important feature of regular expressions, called grouping. You will learn the various types of grouping available in Java regular expressions and how to use them.

Left arrow icon Right arrow icon
Download code icon Download Code

Key benefits

  • Discover regular expressions and how they work
  • Implement regular expressions with Java to your code base
  • Learn to use regular expressions in emails, URLs, paths, and IP addresses

Description

Regular expressions are a powerful tool in the programmer's toolbox and allow pattern matching. They are also used for manipulating text and data. This book will provide you with the know-how (and practical examples) to solve real-world problems using regex in Java. You will begin by discovering what regular expressions are and how they work with Java. This easy-to-follow guide is a great place from which to familiarize yourself with the core concepts of regular expressions and to master its implementation with the features of Java 9. You will learn how to match, extract, and transform text by matching specific words, characters, and patterns. You will learn when and where to apply the methods for finding patterns in digits, letters, Unicode characters, and string literals. Going forward, you will learn to use zero-length assertions and lookarounds, parsing the source code, and processing the log files. Finally, you will master tips, tricks, and best practices in regex with Java.

Who is this book for?

This book is for Java developers who would like to understand and use regular expressions. A basic knowledge of Java is assumed.

What you will learn

  • Understand the semantics, rules, and core concepts of writing Java code involving regular expressions
  • Learn about the java.util.Regex package using the Pattern class, Matcher class, code snippets, and more
  • Match and capture text in regex and use back-references to the captured groups
  • Explore Regex using Java String methods and regex capabilities in the Java Scanner API
  • Use zero-width assertions and lookarounds in regex
  • Test and optimize a poorly performing regex and various other performance tips

Product Details

Country selected
Publication date, Length, Edition, Language, ISBN-13
Publication date : Jul 25, 2017
Length: 158 pages
Edition : 1st
Language : English
ISBN-13 : 9781787288706
Vendor :
Oracle
Category :
Languages :

What do you get with a Packt Subscription?

Free for first 7 days. $19.99 p/m after that. Cancel any time!
Product feature icon Unlimited ad-free access to the largest independent learning library in tech. Access this title and thousands more!
Product feature icon 50+ new titles added per month, including many first-to-market concepts and exclusive early access to books as they are being written.
Product feature icon Innovative learning tools, including AI book assistants, code context explainers, and text-to-speech.
Product feature icon Thousands of reference materials covering every tech concept you need to stay up to date.
Subscribe now
View plans & pricing

Product Details

Publication date : Jul 25, 2017
Length: 158 pages
Edition : 1st
Language : English
ISBN-13 : 9781787288706
Vendor :
Oracle
Category :
Languages :

Packt Subscriptions

See our plans and pricing
Modal Close icon
$19.99 billed monthly
Feature tick icon Unlimited access to Packt's library of 7,000+ practical books and videos
Feature tick icon Constantly refreshed with 50+ new titles a month
Feature tick icon Exclusive Early access to books as they're written
Feature tick icon Solve problems while you work with advanced search and reference features
Feature tick icon Offline reading on the mobile app
Feature tick icon Simple pricing, no contract
$199.99 billed annually
Feature tick icon Unlimited access to Packt's library of 7,000+ practical books and videos
Feature tick icon Constantly refreshed with 50+ new titles a month
Feature tick icon Exclusive Early access to books as they're written
Feature tick icon Solve problems while you work with advanced search and reference features
Feature tick icon Offline reading on the mobile app
Feature tick icon Choose a DRM-free eBook or Video every month to keep
Feature tick icon PLUS own as many other DRM-free eBooks or Videos as you like for just $5 each
Feature tick icon Exclusive print discounts
$279.99 billed in 18 months
Feature tick icon Unlimited access to Packt's library of 7,000+ practical books and videos
Feature tick icon Constantly refreshed with 50+ new titles a month
Feature tick icon Exclusive Early access to books as they're written
Feature tick icon Solve problems while you work with advanced search and reference features
Feature tick icon Offline reading on the mobile app
Feature tick icon Choose a DRM-free eBook or Video every month to keep
Feature tick icon PLUS own as many other DRM-free eBooks or Videos as you like for just $5 each
Feature tick icon Exclusive print discounts

Frequently bought together


Stars icon
Total $ 142.97
Distributed Computing in Java 9
$48.99
Java 9 Regular Expressions
$38.99
Java 9 Programming Blueprints
$54.99
Total $ 142.97 Stars icon

Table of Contents

8 Chapters
Getting Started with Regular Expressions Chevron down icon Chevron up icon
Understanding the Core Constructs of Java Regular Expressions Chevron down icon Chevron up icon
Working with Groups, Capturing, and References Chevron down icon Chevron up icon
Regular Expression Programming Using Java String and Scanner APIs Chevron down icon Chevron up icon
Introduction to Java Regular Expression APIs - Pattern and Matcher Classes Chevron down icon Chevron up icon
Exploring Zero-Width Assertions, Lookarounds, and Atomic Groups Chevron down icon Chevron up icon
Understanding the Union, Intersection, and Subtraction of Character Classes Chevron down icon Chevron up icon
Regular Expression Pitfalls, Optimization, and Performance Improvements Chevron down icon Chevron up icon

Customer reviews

Rating distribution
Full star icon Full star icon Full star icon Full star icon Full star icon 5
(2 Ratings)
5 star 100%
4 star 0%
3 star 0%
2 star 0%
1 star 0%
Amazon Customer Jun 08, 2018
Full star icon Full star icon Full star icon Full star icon Full star icon 5
This book is enough to have an in depth knowledge in Regular expressions.
Amazon Verified review Amazon
Justin Apr 19, 2018
Full star icon Full star icon Full star icon Full star icon Full star icon 5
Amazingly insightful, here is a man that really knows his stuff and will help you figure out the most in depth problems. I would recommend this to anyone who actually wants to know how to solve problems with regex.
Amazon Verified review Amazon
Get free access to Packt library with over 7500+ books and video courses for 7 days!
Start Free Trial

FAQs

What is included in a Packt subscription? Chevron down icon Chevron up icon

A subscription provides you with full access to view all Packt and licnesed content online, this includes exclusive access to Early Access titles. Depending on the tier chosen you can also earn credits and discounts to use for owning content

How can I cancel my subscription? Chevron down icon Chevron up icon

To cancel your subscription with us simply go to the account page - found in the top right of the page or at https://subscription.packtpub.com/my-account/subscription - From here you will see the ‘cancel subscription’ button in the grey box with your subscription information in.

What are credits? Chevron down icon Chevron up icon

Credits can be earned from reading 40 section of any title within the payment cycle - a month starting from the day of subscription payment. You also earn a Credit every month if you subscribe to our annual or 18 month plans. Credits can be used to buy books DRM free, the same way that you would pay for a book. Your credits can be found in the subscription homepage - subscription.packtpub.com - clicking on ‘the my’ library dropdown and selecting ‘credits’.

What happens if an Early Access Course is cancelled? Chevron down icon Chevron up icon

Projects are rarely cancelled, but sometimes it's unavoidable. If an Early Access course is cancelled or excessively delayed, you can exchange your purchase for another course. For further details, please contact us here.

Where can I send feedback about an Early Access title? Chevron down icon Chevron up icon

If you have any feedback about the product you're reading, or Early Access in general, then please fill out a contact form here and we'll make sure the feedback gets to the right team. 

Can I download the code files for Early Access titles? Chevron down icon Chevron up icon

We try to ensure that all books in Early Access have code available to use, download, and fork on GitHub. This helps us be more agile in the development of the book, and helps keep the often changing code base of new versions and new technologies as up to date as possible. Unfortunately, however, there will be rare cases when it is not possible for us to have downloadable code samples available until publication.

When we publish the book, the code files will also be available to download from the Packt website.

How accurate is the publication date? Chevron down icon Chevron up icon

The publication date is as accurate as we can be at any point in the project. Unfortunately, delays can happen. Often those delays are out of our control, such as changes to the technology code base or delays in the tech release. We do our best to give you an accurate estimate of the publication date at any given time, and as more chapters are delivered, the more accurate the delivery date will become.

How will I know when new chapters are ready? Chevron down icon Chevron up icon

We'll let you know every time there has been an update to a course that you've bought in Early Access. You'll get an email to let you know there has been a new chapter, or a change to a previous chapter. The new chapters are automatically added to your account, so you can also check back there any time you're ready and download or read them online.

I am a Packt subscriber, do I get Early Access? Chevron down icon Chevron up icon

Yes, all Early Access content is fully available through your subscription. You will need to have a paid for or active trial subscription in order to access all titles.

How is Early Access delivered? Chevron down icon Chevron up icon

Early Access is currently only available as a PDF or through our online reader. As we make changes or add new chapters, the files in your Packt account will be updated so you can download them again or view them online immediately.

How do I buy Early Access content? Chevron down icon Chevron up icon

Early Access is a way of us getting our content to you quicker, but the method of buying the Early Access course is still the same. Just find the course you want to buy, go through the check-out steps, and you’ll get a confirmation email from us with information and a link to the relevant Early Access courses.

What is Early Access? Chevron down icon Chevron up icon

Keeping up to date with the latest technology is difficult; new versions, new frameworks, new techniques. This feature gives you a head-start to our content, as it's being created. With Early Access you'll receive each chapter as it's written, and get regular updates throughout the product's development, as well as the final course as soon as it's ready.We created Early Access as a means of giving you the information you need, as soon as it's available. As we go through the process of developing a course, 99% of it can be ready but we can't publish until that last 1% falls in to place. Early Access helps to unlock the potential of our content early, to help you start your learning when you need it most. You not only get access to every chapter as it's delivered, edited, and updated, but you'll also get the finalized, DRM-free product to download in any format you want when it's published. As a member of Packt, you'll also be eligible for our exclusive offers, including a free course every day, and discounts on new and popular titles.