Book description
Solve real world problems using Regex in Java.
About This Book
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
Who This Book Is 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
In Detail
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.
Style and approach
This book will take readers through this learning journey using simple, easy-to-understand, step-by-step instructions and hands-on examples at every stage.
Table of contents
- Preface
-
Getting Started with Regular Expressions
- Introduction to regular expressions
- Summary
-
Understanding the Core Constructs of Java Regular Expressions
- Understanding the core constructs of regular expressions
-
Quantifiers
- Basic quantifiers
- Greedy versus reluctant (lazy) matching using quantifiers
- Possessive quantifiers
- Boundary constructs
- Character classes
- Escaping special regex metacharacters and escaping rules inside the character classes
- Predefined shorthand character classes
-
Unicode support in Java regular expressions
- Commonly used Unicode character properties
- Negation of the preceding regex directives
- Unicode scripts support
- Double escaping in a Java String when defining regular expressions
- Embedded regular expression mode modifiers
- The placement of embedded modes in a Java regular expression
- Disabling mode modifiers
- Summary
- Working with Groups, Capturing, and References
-
Regular Expression Programming Using Java String and Scanner APIs
- Introduction to the Java String API for regular expressions' evaluation
- Using regular expressions in Java Scanner API
- Summary
- Introduction to Java Regular Expression APIs - Pattern and Matcher Classes
- Exploring Zero-Width Assertions, Lookarounds, and Atomic Groups
- Understanding the Union, Intersection, and Subtraction of Character Classes
-
Regular Expression Pitfalls, Optimization, and Performance Improvements
-
Common pitfalls and ways to avoid them while writing regular expressions
- Do not forget to escape regex metacharacters outside a character class
- Avoid escaping every non-word character
- Avoid unnecessary capturing groups to reduce memory consumption
- However, don't forget to use the required group around alternation
- Use predefined character classes instead of longer versions
- Use the limiting quantifier instead of repeating a character or pattern multiple times
- Do not use an unescaped hyphen in the middle of a character class
- The mistake of calling matcher.goup() without a prior call to matcher.find(), matcher.matches(), or matcher.lookingAt()
- Do not use regular expressions to parse XML / HTML data
- How to test and benchmark your regular expression performance
- Catastrophic or exponential backtracking
-
Optimization and performance enhancement tips
- Use a compiled form of regular expressions
- Use a negated character class instead of the greedy and slow .* or .+
- Avoid unnecessary grouping
- Use lazy quantifiers strategically instead of greedy quantifiers that cause excessive backtracking
- Make use of possessive quantifiers to avoid backtracking
- Extract common repeating substrings out of alternation
- Use atomic group to avoid backtracking and fail fast
- Summary
-
Common pitfalls and ways to avoid them while writing regular expressions
Product information
- Title: Java 9 Regular Expressions
- Author(s):
- Release date: July 2017
- Publisher(s): Packt Publishing
- ISBN: 9781787288706
You might also like
book
Regex Quick Syntax Reference: Understanding and Using Regular Expressions
This quick guide to regular expressions is a condensed code and syntax reference for an important …
video
Understanding Regular Expressions
The regular expression (regex) has been around for over 60 years and it's still poorly understood—and …
book
Mastering Microservices with Java 9 - Second Edition
Master the art of implementing scalable microservices in your production environment with ease About This Book …
book
Java EE 8 Application Development
Develop Enterprise Java applications compliant with the latest version of the Java EE specification About This …