Book Image

Java 9 Regular Expressions

By : Anubhava Srivastava
Book Image

Java 9 Regular Expressions

By: Anubhava Srivastava

Overview of this book

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.
Table of Contents (15 chapters)
Title page
Credits
About the Author
About the Reviewer
www.PacktPub.com
Customer Feedback
Preface
Free Chapter
1
Getting Started with Regular Expressions

Preface

 

In today's information technology world, the size of data is growing by leaps and bounds. IT organizations are processing and storing huge amounts of textual data collected from various sources, such as user actions, leads, searches, shopping data, page views, page hits, and various other forms of user interactions. Search algorithms are used for parsing large texts to extract meaningful pertinent information. Regular expressions are the backbone of many such search algorithms. Regular expressions (or regex in short) are everywhere these days. All the modern programming languages are shipped with a regex module or library to allow programmers to write regex-based programs. 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. This easy-to-follow regex book is a great place for you to familiarize yourself with the core concepts of regular expressions and to master their implementation with the new features of Java 9. You will learn to match, extract, and transform text by matching specific words, characters, and patterns. Readers will learn how to write efficient regular expressions for solving day-to-day problems involving text-based data.

What this book covers

Chapter 1, Getting Started with Regular Expressions, teaches what regular expressions are, what problems are best solved using regular expressions, and the rules to follow while writing them.

Chapter 2, Understanding the Core Constructs of Java Regular Expressions, covers quantifiers, anchors, boundary matchers, and all the available character classes and properties in Java. We will also learn Unicode text matching using regex in Java.

Chapter 3, Working with Groups, Capturing, and References, explores how to match and capture text in regex, the various types of groups available to us, the naming and numbering of a captured group, and how we should use back-reference for the captured groups.

Chapter 4, Regular Expression Programming Using Java String and Scanner APIs, introduces Java regex using Java String methods, and we will move on to regex capabilities in the Java Scanner API.

Chapter 5, Introduction to Java Regular Expression APIs - Pattern and Matcher Classes, discusses the dedicated Java APIs, java.util.regex.Pattern and java.util.regex.Matcher, for complete regex capabilities.

Chapter 6, Exploring Zero-width Assertions, Lookarounds, and Atomic Groups, focuses on zero-width assertions in regex. The chapter covers various zero-width assertions and their usages. We will then move on to learn the important topic of lookarounds in regex.

Chapter 7, Understanding the Union, Intersection, and Subtraction of Character Classes, says that the Java language has added the features of using the intersection and union of character classes in regex. This chapter covers these features.

Chapter 8, Regular Expression Pitfalls, Optimization, and Performance Improvements, explains how to test and optimize a poorly performing regex and various other performance tips.

What you need for this book

The software in this book was tested on Java version 9 on Ubuntu version 16.10. However, all the examples can also be run on the Windows and macOS X operating systems.

Who this book is for

This book is for Java developers who would like to understand and use regular expressions. If you are dealing with text processing problems, such as text validation, searching, and text manipulation, then learning regex is very important for you to make your job easier. This book does not expect readers to have any prior regex knowledge, since the book will cover every aspect of regex, starting from the absolute basics of regex. However, a basic knowledge of Java is assumed in order to be able to write and execute the example programs provided in the book.

Conventions

In this book, you will find a number of text styles that distinguish between different kinds of information. Here are some examples of these styles and an explanation of their meaning.

Code words in text, database table names, folder names, filenames, file extensions, pathnames, dummy URLs, user input, and Twitter handles are shown as follows: "The next lines of code read the link and assign it to the to the BeautifulSoup function."

A block of code is set as follows:

package example.regex;
public class StringMatches 
{ 
  public static void main(String[] args) 
}

When we wish to draw your attention to a particular part of a code block, the relevant lines or items are set in bold:

[default]
exten => s,1,Dial(Zap/1|30)
exten => s,2,Voicemail(u100) 
exten => s,102,Voicemail(b100) 
exten => i,1,Voicemail(s0)

Any command-line input or output is written as follows:

C:\Python34\Scripts> pip install -upgrade pipC:\Python34\Scripts> pip install pandas

New terms and important words are shown in bold. Words that you see on the screen, for example, in menus or dialog boxes, appear in the text like this: "In order to download new modules, we will go to Files | Settings | Project Name | Project Interpreter."

Note

Warnings or important notes appear like this.

Note

Tips and tricks appear like this.

Reader feedback

Feedback from our readers is always welcome. Let us know what you think about this book-what you liked or disliked. Reader feedback is important for us as it helps us develop titles that you will really get the most out of.

To send us general feedback, simply e-mail [email protected], and mention the book's title in the subject of your message. If there is a topic that you have expertise in and you are interested in either writing or contributing to a book, see our author guide at www.packtpub.com/authors.

Customer support

Now that you are the proud owner of a Packt book, we have a number of things to help you to get the most from your purchase.

Downloading the example code

You can download the example code files for this book from your account at http://www.packtpub.com. If you purchased this book elsewhere, you can visit http://www.packtpub.com/support and register to have the files e-mailed directly to you.

You can download the code files by following these steps:

  1. Log in or register to our website using your e-mail address and password.
  2. Hover the mouse pointer on the SUPPORT tab at the top.
  3. Click on Code Downloads & Errata.
  4. Enter the name of the book in the Search box.
  5. Select the book for which you're looking to download the code files.
  6. Choose from the drop-down menu where you purchased this book from.
  7. Click on Code Download.

Once the file is downloaded, please make sure that you unzip or extract the folder using the latest version of:

  • WinRAR / 7-Zip for Windows
  • Zipeg / iZip / UnRarX for Mac
  • 7-Zip / PeaZip for Linux

The code bundle for the book is also hosted on GitHub at https://github.com/PacktPublishing/Java-9-Regular-Expressions. We also have other code bundles from our rich catalog of books and videos available at https://github.com/PacktPublishing/. Check them out!

Errata

Although we have taken every care to ensure the accuracy of our content, mistakes do happen. If you find a mistake in one of our books-maybe a mistake in the text or the code-we would be grateful if you could report this to us. By doing so, you can save other readers from frustration and help us improve subsequent versions of this book. If you find any errata, please report them by visiting http://www.packtpub.com/submit-errata, selecting your book, clicking on the Errata Submission Form link, and entering the details of your errata. Once your errata are verified, your submission will be accepted and the errata will be uploaded to our website or added to any list of existing errata under the Errata section of that title.

To view the previously submitted errata, go to https://www.packtpub.com/books/content/support and enter the name of the book in the search field. The required information will appear under the Errata section.

Piracy

Piracy of copyrighted material on the Internet is an ongoing problem across all media. At Packt, we take the protection of our copyright and licenses very seriously. If you come across any illegal copies of our works in any form on the Internet, please provide us with the location address or website name immediately so that we can pursue a remedy.

Please contact us at [email protected] with a link to the suspected pirated material.

We appreciate your help in protecting our authors and our ability to bring you valuable content.

Questions

If you have a problem with any aspect of this book, you can contact us at [email protected], and we will do our best to address the problem.