Search icon
Subscription
0
Cart icon
Close icon
You have no products in your basket yet
Save more on your purchases!
Savings automatically calculated. No voucher code required
Arrow left icon
All Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Newsletters
Free Learning
Arrow right icon
$9.99 | ALL EBOOKS & VIDEOS
Over 7,000 tech titles at $9.99 each with AI-powered learning assistants on new releases
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

By Anubhava Srivastava
$29.99 $9.99
Book Jul 2017 158 pages 1st Edition
eBook
$29.99 $9.99
Print
$38.99
Subscription
$15.99 Monthly
eBook
$29.99 $9.99
Print
$38.99
Subscription
$15.99 Monthly

What do you get with eBook?

Product feature icon Instant access to your Digital eBook purchase
Product feature icon Download this book in EPUB and PDF formats
Product feature icon Access this title in our online reader with advanced features
Product feature icon DRM FREE - Read whenever, wherever and however you want
Buy Now

Product Details


Publication date : Jul 25, 2017
Length 158 pages
Edition : 1st Edition
Language : English
ISBN-13 : 9781787288706
Vendor :
Oracle
Category :
Table of content icon View table of contents Preview book icon Preview Book

Java 9 Regular Expressions

Chapter 1. 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.

Note

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.

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

What do you get with eBook?

Product feature icon Instant access to your Digital eBook purchase
Product feature icon Download this book in EPUB and PDF formats
Product feature icon Access this title in our online reader with advanced features
Product feature icon DRM FREE - Read whenever, wherever and however you want
Buy Now

Product Details


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

Table of Contents

15 Chapters
Title page Chevron down icon Chevron up icon
Credits Chevron down icon Chevron up icon
About the Author Chevron down icon Chevron up icon
About the Reviewer Chevron down icon Chevron up icon
www.PacktPub.com Chevron down icon Chevron up icon
Customer Feedback Chevron down icon Chevron up icon
Preface Chevron down icon Chevron up icon
1. Getting Started with Regular Expressions Chevron down icon Chevron up icon
2. Understanding the Core Constructs of Java Regular Expressions Chevron down icon Chevron up icon
3. Working with Groups, Capturing, and References Chevron down icon Chevron up icon
4. Regular Expression Programming Using Java String and Scanner APIs Chevron down icon Chevron up icon
5. Introduction to Java Regular Expression APIs - Pattern and Matcher Classes Chevron down icon Chevron up icon
6. Exploring Zero-Width Assertions, Lookarounds, and Atomic Groups Chevron down icon Chevron up icon
7. Understanding the Union, Intersection, and Subtraction of Character Classes Chevron down icon Chevron up icon
8. Regular Expression Pitfalls, Optimization, and Performance Improvements Chevron down icon Chevron up icon

Customer reviews

Filter icon Filter
Top Reviews
Rating distribution
Empty star icon Empty star icon Empty star icon Empty star icon Empty star icon 0
(0 Ratings)
5 star 0%
4 star 0%
3 star 0%
2 star 0%
1 star 0%

Filter reviews by


No reviews found
Get free access to Packt library with over 7500+ books and video courses for 7 days!
Start Free Trial

FAQs

How do I buy and download an eBook? Chevron down icon Chevron up icon

Where there is an eBook version of a title available, you can buy it from the book details for that title. Add either the standalone eBook or the eBook and print book bundle to your shopping cart. Your eBook will show in your cart as a product on its own. After completing checkout and payment in the normal way, you will receive your receipt on the screen containing a link to a personalised PDF download file. This link will remain active for 30 days. You can download backup copies of the file by logging in to your account at any time.

If you already have Adobe reader installed, then clicking on the link will download and open the PDF file directly. If you don't, then save the PDF file on your machine and download the Reader to view it.

Please Note: Packt eBooks are non-returnable and non-refundable.

Packt eBook and Licensing When you buy an eBook from Packt Publishing, completing your purchase means you accept the terms of our licence agreement. Please read the full text of the agreement. In it we have tried to balance the need for the ebook to be usable for you the reader with our needs to protect the rights of us as Publishers and of our authors. In summary, the agreement says:

  • You may make copies of your eBook for your own use onto any machine
  • You may not pass copies of the eBook on to anyone else
How can I make a purchase on your website? Chevron down icon Chevron up icon

If you want to purchase a video course, eBook or Bundle (Print+eBook) please follow below steps:

  1. Register on our website using your email address and the password.
  2. Search for the title by name or ISBN using the search option.
  3. Select the title you want to purchase.
  4. Choose the format you wish to purchase the title in; if you order the Print Book, you get a free eBook copy of the same title. 
  5. Proceed with the checkout process (payment to be made using Credit Card, Debit Cart, or PayPal)
Where can I access support around an eBook? Chevron down icon Chevron up icon
  • If you experience a problem with using or installing Adobe Reader, the contact Adobe directly.
  • To view the errata for the book, see www.packtpub.com/support and view the pages for the title you have.
  • To view your account details or to download a new copy of the book go to www.packtpub.com/account
  • To contact us directly if a problem is not resolved, use www.packtpub.com/contact-us
What eBook formats do Packt support? Chevron down icon Chevron up icon

Our eBooks are currently available in a variety of formats such as PDF and ePubs. In the future, this may well change with trends and development in technology, but please note that our PDFs are not Adobe eBook Reader format, which has greater restrictions on security.

You will need to use Adobe Reader v9 or later in order to read Packt's PDF eBooks.

What are the benefits of eBooks? Chevron down icon Chevron up icon
  • You can get the information you need immediately
  • You can easily take them with you on a laptop
  • You can download them an unlimited number of times
  • You can print them out
  • They are copy-paste enabled
  • They are searchable
  • There is no password protection
  • They are lower price than print
  • They save resources and space
What is an eBook? Chevron down icon Chevron up icon

Packt eBooks are a complete electronic version of the print edition, available in PDF and ePub formats. Every piece of content down to the page numbering is the same. Because we save the costs of printing and shipping the book to you, we are able to offer eBooks at a lower cost than print editions.

When you have purchased an eBook, simply login to your account and click on the link in Your Download Area. We recommend you saving the file to your hard drive before opening it.

For optimal viewing of our eBooks, we recommend you download and install the free Adobe Reader version 9.