A simple check whether a string contains a substring matching our regular expression is not the only thing we can do with a Matcherobject. It also provides us with additional information about matches, which is essential in some tasks.
The Java Class Library has two special classes possessing advanced features for work with regular expressions: java.util.regex.Pattern and java.util.regex.Matcher. A Matcher object provides us with many useful methods to handle regexes, while a Pattern object represents a regular expression itself.
Regular expressions have a very wide scope of application. They’re used in text editors and in implementations of programming languages, for parsing and syntax highlighting, for extraction of useful information from files and web sites. In this lesson, we would like to show you an example of a simple yet powerful program using a regular expression for string processing.
There is a type of characters called quantifiers, which defines how often another character can occur in a regex pattern. A quantifier can be written after a regular character, as well as after a special one. In general, quantifiers are one of the most essential and important features of the regex language, since they allow a single pattern to match different strings varying in length.
The dot character allows us to write common patterns for matching. The dot, though, matches almost every possible character, and sometimes we want to be more specific in our regex. Then, the sets come to our rescue.
A regular expression is a sequence of characters that specifies a set of strings and that is used to search, edit, and manipulate text. Like most programming languages Java supports regular expressions. We’ve already learned some basics of the regex language. In this lesson, we’ll explore how it is realized in Java.
There are some sets that are used more often than the other ones: sets for digits, or alphanumeric characters, or whitespace characters (there are quite a lot of whitespace characters, we must say). To make the usage of such sets easier and quicker, there are special shorthands, which are equivalent to certain sets, but have shorter “names”.
Sometimes we may need to replace a substring with another string. Java provides us with several convenient methods to do this, and regular expressions play an important role in this process.