Regular Expressions
Now it’s time to take a brief detour on our trip through Java and
enter the land of regular expressions. A regular
expression, or regex for short, describes a text pattern. Regular
expressions are used with many tools—including the java.util.regex package,
text editors, and many scripting languages—to provide sophisticated
text-searching and powerful string-manipulation capabilities.
If you are already familiar with the concept of regular expressions and how they are used with other languages, you may wish to skim through this section. At the very least, you’ll need to look at the “The java.util.regex API” section later in this chapter, which covers the Java classes necessary to use them. On the other hand, if you’ve come to this point on your Java journey with a clean slate on this topic and you’re wondering exactly what regular expressions are, then pop open your favorite beverage and get ready. You are about to learn about the most powerful tool in the arsenal of text manipulation and what is, in fact, a tiny language within a language, all in the span of a few pages.
Regex Notation
A regular expression describes a pattern in text. By pattern, we mean just about any feature you can imagine identifying in text from the literal characters alone, without actually understanding their meaning. This includes features, such as words, word groupings, lines and paragraphs, punctuation, case, and more generally, strings and numbers with a specific structure to them, such ...