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

The Matcher class


An instance of the Matcher class performs various match operations on a character sequence by interpreting a compiled regular expression represented by an instance of Pattern. This is how we use this class to match a regex:

  • We create a matcher instance from a pattern by invoking the pattern's matcher method that requires the input sequence as argument
  • The instance of matcher is used to perform three types of match operations using these three methods, each returning a Boolean value (true indicates success):
    • matches
    • find
    • lookingAt

These methods perform the matching in the following manner:

  • The matches method attempts to match the complete input sequence using the matcher's pattern
  • The find method searches the input sequence for the next substring that matches the pattern
  • The lookingAt method attempts to match the input sequence using the matcher's pattern at the start position.

Let's list down all the important methods from the Matcher class here:

Method Signature

Description

boolean...