
- 158 pages
- English
- ePUB (mobile friendly)
- Available on iOS & Android
Java 9 Regular Expressions
About this book
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 addressesWho This Book Is ForThis 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 tipsIn DetailRegular 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 approachThis book will take readers through this learning journey using simple, easy-to-understand, step-by-step instructions and hands-on examples at every stage.
Tools to learn more effectively

Saving Books

Keyword Search

Annotating Text

Listen to it instead
Information
Understanding the Core Constructs of Java Regular Expressions
- Anchors and quantifiers
- Boundary matchers
- Character classes
- Regex escaping rules
- Escaping inside character classes
- Negated character classes
- Predefined character classes
- Unicode characters matching
- Greedy quantifiers
- Lazy quantifiers
- Possessive quantifiers
- Various embedded modes in regular expressions and their meaning
- Enabling/disabling regular expression modes inside the regex
Understanding the core constructs of regular expressions
| Special Character | Meaning |
| \0c | A character with the octal value c |
| \0cc | A character with the octal value cc |
| \0ncc | A character with the octal value ncc, where n cannot be more than 3 |
| \xhh | A character with the hexadecimal value 0xhh |
| \uhhhh | A character with the hexadecimal value 0xhhhh |
| \x{h...h} | A character with the hexadecimal value 0xh...h, where h must be a valid CODE_POINT |
| \n | Newline character or u000A |
| \t | Tab character or u0009 |
| \r | Carriage return character or u000D |
| \f | Form feed character or u000C |
| \e | Escape character or u\u001B |
| \a | Bell character or \u0007 |
| \cn | A control character represented by n |
Quantifiers
Basic quantifiers
| Quantifier | Meaning |
| m* | Match m zero or more times |
| m+ | Match m one or more times |
| m? | Match m one or zero times (also called an optional match) |
| m{X} | Match m exactly X times |
| m{X,} | Match m X or more times |
| m{X,Y} | Match m at least X and at most Y times |
Examples using quantifiers
\d{2}|\d{4} ^[+-]?\d*\.?\d+$
- The ^ and $ symbols are the start/end anchors
- The [+-]? pattern makes either the + sign or the - sign (optional because of ?) at the start
- The \d* pattern matches zero or more digits
- The \.? pattern matches an optional dot (.) literally
- The \d+ pattern matches one or more digits
- .45
- 123789
- 5
- 123.45
- +67.66
- -987.34
^\d{2,4}$ ^[+-]?\d{7}$ Greedy versus reluctant (lazy) matching using quantifiers
Table of contents
- Title page
- Copyright
- Credits
- About the Author
- About the Reviewer
- www.PacktPub.com
- Customer Feedback
- Preface
- Getting Started with Regular Expressions
- Understanding the Core Constructs of Java Regular Expressions
- Working with Groups, Capturing, and References
- Regular Expression Programming Using Java String and Scanner APIs
- 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
Frequently asked questions
- Essential is ideal for learners and professionals who enjoy exploring a wide range of subjects. Access the Essential Library with 800,000+ trusted titles and best-sellers across business, personal growth, and the humanities. Includes unlimited reading time and Standard Read Aloud voice.
- Complete: Perfect for advanced learners and researchers needing full, unrestricted access. Unlock 1.4M+ books across hundreds of subjects, including academic and specialized titles. The Complete Plan also includes advanced features like Premium Read Aloud and Research Assistant.
Please note we cannot support devices running on iOS 13 and Android 7 or earlier. Learn more about using the app