Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
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
€18.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
€15.99 €23.99
Paperback
€29.99
Subscription
Free Trial
Renews at €18.99p/m
Arrow left icon
Profile Icon Anubhava Srivastava
Arrow right icon
€18.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
€15.99 €23.99
Paperback
€29.99
Subscription
Free Trial
Renews at €18.99p/m
eBook
€15.99 €23.99
Paperback
€29.99
Subscription
Free Trial
Renews at €18.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

Getting Started with Regular Expressions

In this chapter, you will be introduced to regular expressions (or regex in short). You will learn about some real-world problems that can be solved by using regular expressions and the basic building blocks of regular expressions.

We will be covering the following topics in this chapter:

  • Introduction to regular expressions
  • A brief history of regular expressions
  • The various flavors of regular expressions
  • What type of problems need regular expressions to solve
  • The basic rules of writing regular expressions
  • Standard regular expression meta characters
  • Basic regular expression examples

Introduction to regular expressions

Regular expression (or in short regex) is a very useful tool that is used to describe a search pattern for matching the text. Regex is nothing but a sequence of some characters that defines a search pattern. Regex is used for parsing, filtering, validating, and extracting meaningful information from large text, such as logs and output generated from other programs.

We find regular expressions in day-to-day use on many websites. For example, while searching for your favorite recipe on search engines, while filling up forms and entering data such as username and passwords, and so on. While setting up a password on many sites, we encounter password validation errors, such as password must contain one digit or at least one uppercase letter or at least one special character, and so on. All these checks can be done using regular expressions. A few more typical examples of regular expressions are validating phone numbers or validating postal/zip/pin codes.

A bit of history of regular expressions

Renowned mathematician Stephen Kleene built a model in the year 1956 using finite automata for simple algebra. He described regular languages using his mathematical notation called regular sets. Computer programmers started using regular expressions in the 1970s when the Unix operating system and some of its text editors and text processing utilities such as ed, sed, emacs, lex, vi, grep, awk, and so on were built. Regular expressions gained more popularity with the arrival of Perl and Tcl scripting languages in the 1980s and 1990s. Since then, all the popular programming languages, such as Java, Python, Ruby, R, PHP, and .NET have built very good support of regular expressions.

Various flavors of regular expressions

All the programming and scripting languages have built-in support for regular expressions these days. The basic rules to define and execute regular expressions are pretty much the same across all the languages. However, these regex implementations have their own flavors that differ from each other at the advanced level. We will cover regular expressions using Java in this book.

Some of the popular flavors of regular expressions are as follows:

  • .NET
  • Java
  • Perl
  • PCRE (PHP)
  • JavaScript
  • VBScript
  • Python
  • R
  • Ruby
  • std::regex
  • boost::regex
  • Basic Regular Expressions (BRE) - used by Unix utilities ed, vi, sed, grep, and so on
  • Extended Regular Expressions (ERE) - used by Unix utilities sed, grep, awk, and so on

What type of problems need regular expressions to solve

Some programmers wonder why they even need to learn regular expressions. Here are some use cases:

  • While searching for some text at times, there are cases where we don't know the value of the text upfront. We just know some rules or patterns of the text. For example, searching for a MAC address in a log message, searching for IP address in a web server access log, or searching for a 10-digit mobile number that may be optionally preceded by 0 or +<2 digit country code>.
  • Sometimes, the length of the text we are trying to extract is unknown, for example, searching URLs that start with http:// or https:// in a CSV file.
  • Sometimes, we need to split a given text on delimiters of a variable type and length and generate tokens.
  • Sometimes, we need to extract text that falls between two or more search patterns.
  • Often, we need to validate the various forms of user inputs, such as bank account number, passwords, usernames, credit card info, phone number, date of birth, and so on.
  • There are situations where you only want to capture all the repeated words from a line.
  • To convert input text into certain predefined formats, such as inserting a comma after every three digits or removing commas inside parentheses only.
  • To do a global search replace while skipping all the escaped characters.

The basic rules of regular expressions

Many of you are familiar with wild cards (in the Unix world, it is called glob pattern) matching of text. Here:

  • ? matches any single character
  • * matches any sequence of characters
  • [abc] matches any one character inside square brackets, so it will match a, b, or c

The regular expression pattern goes many steps farther than wild cards, where one can set many rules in a regex pattern, such as the following:

  • Match a character or a group of characters optionally (0 or 1 times)
  • Use quantifiers in regex patterns to match variable length text
  • Use a character class to match one of the listed characters or match a range of characters
  • Use a negated character class to match any character except those matched by the character class
  • Match only certain character categories, such as match only digits, only upper case letters, or only punctuation characters
  • Match a character or a group of characters for a specific length.
  • Match a length range, such as allow only six to 10 digits in the input or match an input of a minimum of eight characters
  • Use Boolean "OR" in an alternation to match one of the few alternative options
  • Use groups in regex patterns and capture substrings that we want to extract or replace from a given input
  • Alter the behavior of matching by keeping it greedy (eager), lazy (reluctant), or possessive
  • Use back references and forward references of groups that we capture
  • Use zero-width assertions such as the following:
    • Start and end anchors
    • Word boundary
    • Lookahead and lookbehind assertions
    • Start a match from the end of a previous match

For example, in a regex to match a or b we can use the following alternation:

a|b

To match one or more instances of the digit 5, we can use the following:

5+

To match any substring that starts with p and ends with w, we can use the following:

p.*w

Constructs of the standard regular expression and meta characters

Let's get familiar with core constructs of regular expressions and some reserve meta characters that have a special meaning in regular expressions. We shall cover these constructs in detail in the coming chapters:

Symbol Meaning Example
. (dot or period) Matches any character other than newline. Matches #, @, A, f, 5, or .
* (asterisk) * matches zero or more occurrences of the preceding character or group. m* matches 0 or more occurrences of the letter m.
+ (plus) + matches one or more occurrences of the preceding element. m+ matches one or more occurrences of the letter m.
? (question mark) ? means optional match. It is used to match zero or one occurrence of the preceding element. It is also used for lazy matching (which will be covered in the coming chapters). nm? means match n or nm, as m is an optional match here.
| (pipe) | means alternation. It is used to match one of the elements separated by | m|n|p means match either the letter m or the letter n or the letter p
^ (cap) ^ is called anchor, that matches start of the line ^m matches m only when it is the first character of the string that we are testing against the regular expression. Also, note that you do not use ^ in the middle of a regular expression.
$ (dollar) $ is called anchor that matches line end. m$ matches m only at line end.
\b (backslash followed by the letter b) Alphabets, numbers, and underscore are considered word characters. \b asserts word boundary, which is the position just before and after a word.

\bjava\b matches the word, java . So, it will not match javascript since the word, javascript, will fail to assert \b after java in the regex.

\B (backslash followed by uppercase B) \B asserts true where \b doesn't, that is, between two word characters.

For the input text, abc,

\B will be asserted at two places:

  1. Between a and b.
  2. Between b and c.
(...) a sub-pattern inside round parentheses This is for grouping a part of text that can be used to capture a certain substring or for setting precedence. m(ab)*t matches m, followed by zero or more occurrences of the substring, ab, followed by t.
{min,max} A quantifier range to match the preceding element between the minimum and the maximum number. mp{2,4} matches m followed 2 to 4 occurrences of the letter p.
[...] This is called a character class. [A-Z] matches any uppercase English alphabet.
\d (backslash followed by the letter d) This will match any digit. \d matches any digit in the 0-9 range.
\D (backslash followed by uppercase D) This matches any character that is not a digit. \D matches a, $, or _.
\s (backslash followed by the letter s) Matches any whitespace, including tab, space, or newline. \s matches [ \t\n].
\S (backslash followed by uppercase S) Matches any non-whitespace. \S matches the opposite of \s
\w (backslash followed by the letter w) Matches any word character that means all alphanumeric characters or underscore. \w will match [a-zA-Z0-9_], so it will match any of these strings: "abc", "a123", or "pq_12_ABC"
\W (backslash followed by the letter W) Matches any non-word character, including whitespaces. In regex, any character that is not matched by \w can be matched using \W. It will match any of these strings: "+/=", "$", or " !~"

Some basic regular expression examples

Let's look at some basic examples of regular expressions:

    ab*c

This will match a, followed by zero or more b, followed by c.

    ab+c

This will match a followed by one or more b, followed by c.

    ab?c

This will match a followed by zero or one b, followed by c. Thus, it will match both abc or ac.

    ^abc$

This will match abc in a line, and the line must not have anything other than the string abc due to the use of the start and end anchors on either side of the regex.

    a(bc)*z

This will match a, followed by zero or more occurrences of the string bc, followed by z. Thus, it will match the following strings: az, abcz, abcbcz, abcbcbcz, and so on.

    ab{1,3}c

This will match a, followed by one to three occurrences of b, followed by c. Thus, it will match following strings: abc, abbc, and abbbc.

    red|blue

This will match either the string red or the string blue.

    \b(cat|dog)\b

This will match either the string cat or the string dog, ensuring both cat and dog must be complete words; thus, it will fail the match if the input is cats or dogs.

    [0-9]

This is a character class with a character range. The preceding example will match a digit between 0 and 9.

    [a-zA-Z0-9]

This is a character class with a character range. The preceding example will match any alpha-numeric character.

    ^\d+$

This regex will match an input containing only one or more digits.

    ^\d{4,8}$

This regex will allow an input containing four to eight digits only. For example, 1234, 12345, 123456, and 12345678 are valid inputs.

    ^\d\D\d$

This regex not only allows only one digit at the start and end but also enforces that between these two digits there must be one non-digit character. For example, 1-5, 3:8, 8X2, and so on are valid inputs.

    ^\d+\.\d+$

This regex matches a floating point number. For example, 1.23, 1548.567, and 7876554.344 are valid inputs.

    .+

This matches any character one or more times. For example, qwqewe, 12233, or f5^h_=!bg are all valid inputs:

    ^\w+\s+\w+$

This matches a word, followed by one or more whitespaces, followed by another word in an input. For example, hello word, John Smith, and United Kingdom will be matched using this regex.

Engine is a term often used for an underlying module that evaluates the provided regular expression and matches the input string.

Eager matching

At this point, it is important to understand one important behavior of regular expression engines, called eagerness. A regular expression engine performs a match operation from left to right in an input string. While matching a regex pattern against the input string, the regex engine moves from left to right and is always eager to complete a match, even though there are other alternative ways in the regular expression to complete the match. Once a substring is matched, it stops proceeding further and returns the match. Only when a character position fails to match all the possible permutations of the regular expression, then the regex engine moves character by character to attempt a match at the next position in the input string. While evaluating a regex pattern, the regex engine may move backwards (backtrack) one position at a time to attempt matching.

The effect of eager matching on regular expression alternation

This regular expression engine behavior may return unexpected matches in alternation if alternations are not ordered carefully in the regex pattern.

Take an example of this regex pattern, which matches the strings white or whitewash:

white|whitewash

While applying this regex against an input of whitewash, the regex engine finds that the first alternative white matches the white substring of the input string whitewash, hence, the regex engine stops proceeding further and returns the match as white.

Note that our regex pattern has a better second alternative as whitewash, but due to the regex engine's eagerness to complete and return the match, the first alternative is returned as a match and the second alternative is ignored.

However, consider swapping the positions of the third and fourth alternatives in our regex pattern to make it as follows:

whitewash|white

If we apply this against the same input, whitewash, then the regex engine correctly returns the match as whitewash.

We can also use anchors or boundary matchers in our regular expressions to make it match a complete word. Any of the following two patterns will match and return whitewash as a match:

^(white|whitewash)$

\b(white|whitewash)\b

Let's take a look at a more interesting example, which attempts to match a known literal string "cat & rat" or a complete word in the input, using the following pattern:

\b(\w+|cat & rat)\b

If the input string is story of cat & rat, and we apply our regex pattern repeatedly, then the following four matched substrings will be returned:

1. story
2. of
3. cat
4. rat

It is because the regex engine is eagerly using the first alternative pattern \w+ to match a complete word and is returning all the matched words. The engine never attempts a second alternative of the literal string, cat & rat, because a successful match is always found using the first alternative. However, let's change the regex pattern to the following:

\b(cat & rat|\w+)\b

If we apply this regex on the same sting, story of cat & rat, and we apply our regex pattern repeatedly, then the following three matched substrings will be returned:

1. story
2. of
3. cat & rat

This is because now cat & rat is the first alternative and when the regex engine moves to a position before the letter c in the input, it is able to match and return a successful match using the first alternative.

Summary

In this chapter, you were introduced to regular expressions with a bit of history and their flavors. You learnt some use cases where regex are needed. Finally, we covered the basic rules and building blocks of writing regex, with a few examples. You also learnt the eager-matching behavior of the regex engine and how it may impact matching in alternations.

In the next chapter, we will go a level deeper and cover the core concepts of regex in detail, such as quantifiers, lazy vs greedy matching, anchors, negated character classes, Unicode and predefined character classes, special escape sequences, and the rules of escaping inside a character class.

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
€18.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
€189.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
€264.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 108.97
Distributed Computing in Java 9
€36.99
Java 9 Regular Expressions
€29.99
Java 9 Programming Blueprints
€41.99
Total 108.97 Stars icon
Banner background image

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.